Play WAV files using Java

The following Java code plays a .wav file

import java.io.File;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;
 
public class MusicPlayer {
 public static void main(String args[]) {
    try {
         AudioInputStream stream =
                  AudioSystem.getAudioInputStream(
                    new File("C:\\i386\\Blip.wav"));
 
 	AudioFormat format = stream.getFormat();
        DataLine.Info info =
                  new DataLine.Info(Clip.class,
                          stream.getFormat());
        Clip clip = (Clip) AudioSystem.getLine(info);
 
        clip.open(stream);
        clip.start();
 
      } catch (Exception e) {
           e.printStackTrace();
      }
   }
 
}
Did you like this? If so, please
tell a friend
about it, and subscribe to the blog RSS feed.

Share/Save/Bookmark

If you enjoyed this post, make sure you subscribe to my RSS feed!



Related Posts:
  • Recursively delete a directory
  • Sending an e-mail using WebSphere Mail session settings.
  • Sorting an ArrayList of objects.
  • LIKE clause with PreparedStatement
  • Exception stackTrace to a String Variable


  • One Response to “Play WAV files using Java”  

    1. 1 Fredy

      Thanks a lot! Good code.

    Leave a Reply