Advertisement
rootUser

Play sound (.wav file-built in support)

Jul 9th, 2016
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 1.30 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.net.URL;
  3. import javax.sound.sampled.AudioInputStream;
  4. import javax.sound.sampled.AudioSystem;
  5. import javax.sound.sampled.Clip;
  6. import javax.sound.sampled.LineUnavailableException;
  7. import javax.sound.sampled.UnsupportedAudioFileException;
  8. import javax.swing.JFrame;
  9.  
  10. public class SoundClipTest extends JFrame
  11. {
  12.     // Constructor
  13.     public SoundClipTest() throws LineUnavailableException
  14.     {
  15.         try {
  16.             // Open an audio input stream.
  17.             URL url = this.getClass().getClassLoader().getResource("dog.wav");
  18.             AudioInputStream audioIn = AudioSystem.getAudioInputStream(url);
  19.             // Get a sound clip resource.
  20.             Clip clip = AudioSystem.getClip();
  21.             // Open audio clip and load samples from the audio input stream.
  22.             clip.open(audioIn);
  23.             clip.start();
  24.         }
  25.         catch (UnsupportedAudioFileException e)
  26.         {
  27.             e.printStackTrace();
  28.         }
  29.         catch (IOException e)
  30.         {
  31.             e.printStackTrace();
  32.         }
  33.         catch (LineUnavailableException e)
  34.         {
  35.             e.printStackTrace();
  36.         }
  37.     }
  38.  
  39.     public static void main(String[] args) throws LineUnavailableException
  40.     {
  41.         new SoundClipTest();
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement