turt2live

Untitled

Apr 8th, 2012
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.99 KB | None | 0 0
  1. package com.turt2live.ponies;
  2.  
  3. import java.io.FileInputStream;
  4. import java.io.IOException;
  5.  
  6. import sun.audio.AudioData;
  7. import sun.audio.AudioPlayer;
  8. import sun.audio.AudioStream;
  9. import sun.audio.ContinuousAudioDataStream;
  10.  
  11. import com.turt2live.ponies.engine.File;
  12.  
  13. public class Test {
  14.  
  15.     public static void main(String[] args){
  16.         SoundManager sound = new SoundManager(new File("resources/sampleSound.wav"));
  17.         sound.loopEternally();
  18.         for(int i = 0; i < 5; i++){
  19.             try{
  20.                 Thread.sleep(1000);
  21.             }catch(Exception e){
  22.                 e.printStackTrace();
  23.             }
  24.         }
  25.         sound.stop();
  26.     }
  27.  
  28.     /**
  29.      * Sound manager<br>
  30.      * <i>Cleaned up by turt2live</i>
  31.      *
  32.      * @author technodork
  33.      */
  34.     private static class SoundManager {
  35.  
  36.         private AudioStream as;
  37.         private ContinuousAudioDataStream loop;
  38.         private File file;
  39.         private boolean playing = false;
  40.  
  41.         /**
  42.          * Creates a new sound manager
  43.          *
  44.          * @param file the audio file
  45.          */
  46.         public SoundManager(File file){
  47.             this.file = file;
  48.         }
  49.  
  50.         /**
  51.          * Plays the Audio File
  52.          */
  53.         public void play(){
  54.             try{
  55.                 as = new AudioStream(new FileInputStream(file));
  56.                 AudioPlayer.player.start(as);
  57.                 playing = true;
  58.             }catch(IOException e){
  59.                 e.printStackTrace();
  60.             }
  61.         }
  62.  
  63.         /**
  64.          * Stops the Audio File
  65.          */
  66.         public void stop(){
  67.             checkPlaying();
  68.             if(!playing){
  69.                 return;
  70.             }
  71.             if(as != null){
  72.                 AudioPlayer.player.stop(as);
  73.             }else if(loop != null){
  74.                 AudioPlayer.player.stop(loop);
  75.             }
  76.         }
  77.  
  78.         /**
  79.          * Loops the sound file eternally
  80.          */
  81.         public void loopEternally(){
  82.             try{
  83.                 AudioStream backgroundMusic = new AudioStream(new FileInputStream(file));
  84.                 AudioData musicData = backgroundMusic.getData();
  85.                 loop = new ContinuousAudioDataStream(musicData);
  86.                 AudioPlayer.player.start(loop);
  87.             }catch(IOException e){
  88.                 e.printStackTrace();
  89.             }
  90.         }
  91.  
  92.         private void checkPlaying(){
  93.             playing = AudioPlayer.player.isAlive() && !AudioPlayer.player.isInterrupted();
  94.         }
  95.  
  96.     }
  97.  
  98. }
Advertisement
Add Comment
Please, Sign In to add comment