atm959

Sound.java

Mar 18th, 2016
248
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.62 KB | None | 0 0
  1. package com.atm959.sourceCode;
  2.  
  3. import java.io.File;
  4. import java.io.IOException;
  5.  
  6. import javax.sound.sampled.AudioInputStream;
  7. import javax.sound.sampled.AudioSystem;
  8. import javax.sound.sampled.Clip;
  9. import javax.sound.sampled.DataLine;
  10. import javax.sound.sampled.LineUnavailableException;
  11. import javax.sound.sampled.UnsupportedAudioFileException;
  12.  
  13. public class Sounds {
  14.    
  15.     File soundFileBgm;
  16.     File soundFileSfx;
  17.     AudioInputStream soundBgm;
  18.     AudioInputStream soundSfx;
  19.     DataLine.Info infoBgm;
  20.     DataLine.Info infoSfx;
  21.     Clip clipBgm;
  22.     Clip clipSfx;
  23.    
  24.     @SuppressWarnings("static-access")
  25.     public void startBgm(String fileName) throws UnsupportedAudioFileException, IOException, LineUnavailableException{
  26.        
  27.         soundFileBgm = new File("bgm/" + fileName);
  28.         soundBgm = AudioSystem.getAudioInputStream(soundFileBgm);
  29.         infoBgm = new DataLine.Info(Clip.class, soundBgm.getFormat());
  30.         clipBgm = (Clip) AudioSystem.getLine(infoBgm);
  31.         clipBgm.open(soundBgm);
  32.         clipBgm.start();
  33.         clipBgm.loop(clipBgm.LOOP_CONTINUOUSLY);
  34.        
  35.     }
  36.    
  37.     public void stopBgm(){
  38.        
  39.         clipBgm.stop();
  40.         clipBgm.close();
  41.        
  42.     }
  43.    
  44.     public void pauseBgm(){
  45.        
  46.         clipBgm.stop();
  47.        
  48.     }
  49.    
  50.     public void resumeBgm(){
  51.        
  52.         clipBgm.start();
  53.        
  54.     }
  55.    
  56.     public void playSfx(String fileName) throws UnsupportedAudioFileException, IOException, LineUnavailableException{
  57.        
  58.         soundFileSfx = new File("sfx/" + fileName);
  59.         soundSfx = AudioSystem.getAudioInputStream(soundFileSfx);
  60.         infoSfx = new DataLine.Info(Clip.class, soundSfx.getFormat());
  61.         clipSfx = (Clip) AudioSystem.getLine(infoSfx);
  62.         clipSfx.open(soundSfx);
  63.         clipSfx.start();
  64.        
  65.     }
  66.    
  67. }
Advertisement
Add Comment
Please, Sign In to add comment