Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.turt2live.ponies;
- import java.io.FileInputStream;
- import java.io.IOException;
- import sun.audio.AudioData;
- import sun.audio.AudioPlayer;
- import sun.audio.AudioStream;
- import sun.audio.ContinuousAudioDataStream;
- import com.turt2live.ponies.engine.File;
- public class Test {
- public static void main(String[] args){
- SoundManager sound = new SoundManager(new File("resources/sampleSound.wav"));
- sound.loopEternally();
- for(int i = 0; i < 5; i++){
- try{
- Thread.sleep(1000);
- }catch(Exception e){
- e.printStackTrace();
- }
- }
- sound.stop();
- }
- /**
- * Sound manager<br>
- * <i>Cleaned up by turt2live</i>
- *
- * @author technodork
- */
- private static class SoundManager {
- private AudioStream as;
- private ContinuousAudioDataStream loop;
- private File file;
- private boolean playing = false;
- /**
- * Creates a new sound manager
- *
- * @param file the audio file
- */
- public SoundManager(File file){
- this.file = file;
- }
- /**
- * Plays the Audio File
- */
- public void play(){
- try{
- as = new AudioStream(new FileInputStream(file));
- AudioPlayer.player.start(as);
- playing = true;
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- /**
- * Stops the Audio File
- */
- public void stop(){
- checkPlaying();
- if(!playing){
- return;
- }
- if(as != null){
- AudioPlayer.player.stop(as);
- }else if(loop != null){
- AudioPlayer.player.stop(loop);
- }
- }
- /**
- * Loops the sound file eternally
- */
- public void loopEternally(){
- try{
- AudioStream backgroundMusic = new AudioStream(new FileInputStream(file));
- AudioData musicData = backgroundMusic.getData();
- loop = new ContinuousAudioDataStream(musicData);
- AudioPlayer.player.start(loop);
- }catch(IOException e){
- e.printStackTrace();
- }
- }
- private void checkPlaying(){
- playing = AudioPlayer.player.isAlive() && !AudioPlayer.player.isInterrupted();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment