[ Team LiB ] Previous Section Next Section

17.3 Playing Sounds with AudioClip

The sound capabilities of modern computer hardware are usually much more advanced than the dumb terminals of yesterday, and typical users expect their computers to make sounds that are prettier than the coarse console bell. Java programs can do this by loading and playing a file of audio data with the java.applet.AudioClip interface. As the package name implies, the AudioClip interface was originally intended only for applets. Since Java 1.0, applets have been able to call their getAudioClip( ) instance method to read an audio file over the network. In Java 1.2, however, the static java.applet.Applet.newAudioClip( ) method was added to allow any application to read audio data from any URL (including local file: URLs). This method and the AudioClip interface make it very easy to play arbitrary sounds from your programs, as demonstrated by Example 17-2.

Invoke PlaySound with the URL of a sound file as its sole argument. If you are using a local file, be sure to prefix the filename with the file: protocol. The types of sound files supported depend on the Java implementation. Sun's default implementation supports .wav, .aiff, and .au files for sampled sound, .mid files for MIDI, and even .rmf files for the MIDI-related, proprietary "Rich Music Format" defined by Beatnik.[1]

[1] There is no guarantee that the RMF format will continue to be supported.

When you run the PlaySound class of Example 17-2, you may notice that the program never exits. Like AWT applications, programs that use Java's sound capabilities start a background thread and do not automatically exit when the main( ) method returns.[2] To make PlaySound better behaved, we need to explicitly call System.exit( ) when the AudioClip has finished playing. But this highlights one of the shortcomings of the AudioClip interface: it allows you to play( ), stop( ), or loop( ) a sound, but it provides no way to track the progress of the sound or find out when it has finished playing. To achieve that level of control over the playback of sound, we need to use the JavaSound API, which we'll consider in the next section.

[2] This is reported to be fixed in Java 1.5.

Example 17-2. PlaySound.java
package je3.sound;

/**
 * Play a sound file from the network using the java.applet.Applet API.
 */
public class PlaySound {
    public static void main(String[  ] args)
        throws java.net.MalformedURLException
    {
        java.applet.AudioClip clip =
            java.applet.Applet.newAudioClip(new java.net.URL(args[0]));
        clip.play( );
    }
}

17.3.1 Finding Music Files

Before you can test PlaySound, you'll need some sound files to play. You probably have lots of sampled audio files sitting around on your computer: they are often bundled with operating systems and applications. Look for files with .wav, .au, or .aif extensions. You may not have MIDI files on your computer, but many MIDI hobbyists make files available for download on the Internet; a quick Internet search will locate many samples you can use.

The JavaSound web page is also worth a visit (http://java.sun.com/products/java-media/sound/). This page includes links to an interesting JavaSound demo application that includes source code and its own set of sampled audio and MIDI files. Also of interest here are free downloadable MIDI soundbank files, which may give you richer MIDI sound.

    [ Team LiB ] Previous Section Next Section