Advertisement
Guest User

Untitled

a guest
Mar 19th, 2015
296
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.21 KB | None | 0 0
  1. public class Music {
  2.     File f;
  3.     AudioInputStream sound;
  4.     DataLine.Info info;
  5.     static Clip clip;
  6.     static FloatControl gainControl;
  7.     SnakePrefs pref;
  8.     static boolean play;
  9.    
  10.    
  11.     public Music(){    
  12.         try{
  13.             pref = new SnakePrefs();
  14.             play = pref.isMusicSwitch();
  15.             int volMusic = pref.getMusicVol();
  16.            
  17.             f = new File("resources/music.wav");
  18.             sound = AudioSystem.getAudioInputStream(f);
  19.  
  20.             // load the sound into memory (a Clip)
  21.             info = new DataLine.Info(Clip.class, sound.getFormat());
  22.             clip = (Clip) AudioSystem.getLine(info);
  23.             clip.open(sound);
  24.             gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN);
  25.             changeVol(volMusic);
  26.             } catch (IOException | UnsupportedAudioFileException | LineUnavailableException e){
  27.                 System.out.println("Exception occurred while playing music:");
  28.                 e.printStackTrace();
  29.                 play = false;
  30.             }
  31.     }
  32.        
  33.     public static void playMusic(){
  34.           if(play){
  35.               clip.loop(Clip.LOOP_CONTINUOUSLY);
  36.               clip.start();
  37.           }
  38.          
  39.     }
  40.    
  41.     public static void stopMusic(){
  42.           clip.stop();
  43.     }
  44.    
  45.     public static void changeVol(int vol){
  46.         float x = (float) -((100 -  (10 * vol)) / 4);  
  47.         gainControl.setValue(x);
  48.     }
  49.  
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement