Java sound example - how to play a sound file in Java

My Java sound/audio example: I'm working on a simple "meditation" application that plays a sound after a certain period of time (the sound of a gong), so I thought I'd share some source code out here that demonstrates how to play a sound file in a Java application like this.

(Note: I initially found this technique described at JavaWorld.com, but the code below is taken from my project.)

In this case the sound file I'm going to play is an "au" file, but I believe this same technique works with most other sound file types as well.

A simple Java "play sound file" example

Here's the source code for my Java sound file example:

import java.io.*;
import sun.audio.*;

/**
 * A simple Java sound file example (i.e., Java code to play a sound file).
 * AudioStream and AudioPlayer code comes from a javaworld.com example.
 * @author alvin alexander, devdaily.com.
 */
public class JavaAudioPlaySoundExample
{
  public static void main(String[] args) 
  throws Exception
  {
    // open the sound file as a Java input stream
    String gongFile = "/Users/al/DevDaily/Projects/MeditationApp/resources/gong.au";
    InputStream in = new FileInputStream(gongFile);

    // create an audiostream from the inputstream
    AudioStream audioStream = new AudioStream(in);

    // play the audio clip with the audioplayer class
    AudioPlayer.player.start(audioStream);
  }
}

As you can see from this source code, it's pretty easy to create a basic Java sound file player.

My "real world" Java sound file code

I tried to make that example very simple so you can just copy that source code and run it, but my actual code is a little bit different, primarily because I need to read my sound file out of a jar file, instead of reading it as a file on a filesystem.

For the sake of completeness, here's the actual method from my current Java application that plays the sound file by reading the file as a resource from the jar file I create when I build my application:

private void playSound() 
{
  try
  {
    // get the sound file as a resource out of my jar file;
    // the sound file must be in the same directory as this class file.
    // the input stream portion of this recipe comes from a javaworld.com article.
    InputStream inputStream = getClass().getResourceAsStream(SOUND_FILENAME);
    AudioStream audioStream = new AudioStream(inputStream);
    AudioPlayer.player.start(audioStream);
  }
  catch (Exception e)
  {
    // a special way i'm handling logging in this application
    if (debugFileWriter!=null) e.printStackTrace(debugFileWriter);
  }
}

As you can see from that Java code, after I retrieve the sound file from my jar file, the process of playing the sound file is the same in this code as it was in my earlier example.

Java sound/audio API

As I mentioned, I'm just learning about the Java Sound/Audio API capabilities, but it's interesting that these classes come from the sun.audio package (instead of something like a java.sound package). As I've tried to find more Java sound examples, and the Javadoc for the classes I used above, I see there's a javax.sound package that may have power than these classes, while using these two classes seems very simple. So much to learn ...

In the meantime, here are a few great Java sound file links I've discovered on this short journey:

The complete Java sound file application

If you'd like the complete source code for this Java "play sound file" application, along with the Ant build script that is used to build the application on a Mac OS X system, it's available here as my free, complete Java Mac (Swing) application.

ClassLoader

Why don't you use a ClassLoader?
Then the Sound File must not be in the same Directory as this Class File.

ClassLoader CLDR = this.getClass().getClassLoader();
InputStream soundName = CLDR.getResourceAsStream("yourDirectory/yourSound.wav");
AudioStream audioStream = new AudioStream(soundName);
AudioPlayer.player.start(audioStream);

Sorry for the long delay on a

Sorry for the long delay on a reply here, but thanks, this is a much better approach. I have no excuses for not using this approach, other than my own laziness. Somewhere along the line I started using the earlier approach, and never looked for a better way. Thanks again!

thankzzzzz

adding an audio file...........Thankzzzzzzzzzz. It s working perfectly

.................

This code helped me.Thanks to you.

Post new comment

The content of this field is kept private and will not be shown publicly.