Class AudioPlayer

java.lang.Object
  extended by AudioPlayer

public class AudioPlayer
extends java.lang.Object

This class contains utility methods for loading and playing audio clips and streams.

General usage is as follows:

First, preload sounds from files, and give those sounds a name:

    AudioPlayer.loadClip("moo", "cow.wav");
    AudioPlayer.loadClip("chirp", "bird.wav");

The various loading methods return true if the requested audio was loaded successfully, and false otherwise. This value can be used, for example, to display an error message:

    if (!AudioPlayer.loadClip("arf", "dog.wav"))
        System.out.println("Could not load dog sound.");

Then, play the sounds by specifying the names they were given, and whether or not the sound should loop (true or false):

    AudioPlayer.play("moo", true);
    AudioPlayer.play("chirp", false);

A playing sound can be stopped as follows:

    AudioPlayer.stop("moo");
    AudioPlayer.stop("chirp");

Sounds only need to be loaded once. Generally, if the sounds you want to load are small and short, it is recommended that you use one of the loadClip methods. For longer sounds that may not fit in memory, such as full songs, you should use one of the loadStream methods.

When done playing sounds (typically when exiting your program), use:

    AudioPlayer.shutdown();

This will stop all playing sounds.


Constructor Summary
AudioPlayer()
           
 
Method Summary
static boolean loadClip(java.lang.String soundName, java.lang.String filename)
          Loads a sound clip from a file and gives it the specified name.
static boolean loadClip(java.lang.String soundName, java.net.URL url)
          Loads a sound clip from a URL and gives it the specified name.
static boolean loadStream(java.lang.String soundName, java.lang.String filename)
          Loads an audio stream from a file and gives it the specified name.
static boolean loadStream(java.lang.String soundName, java.net.URL url)
          Loads an audio stream from a URL and gives it the specified name.
static void play(java.lang.String soundName, boolean loop)
          Plays a sound that has already been loaded by one of the sound loading methods.
static void shutdown()
          Stops all playing sounds and closes all lines and audio input streams.
static void stop(java.lang.String soundName)
          Stops playing the specified sound.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

AudioPlayer

public AudioPlayer()
Method Detail

loadClip

public static boolean loadClip(java.lang.String soundName,
                               java.lang.String filename)
Loads a sound clip from a file and gives it the specified name. This name can be used when calling the play and stop methods. The sound clip is completely loaded into memory, so it is recommended that this method be used for loading small or short sounds. For longer sounds, consider using the loadStream method.

Parameters:
soundName - the name to give this audio stream
filename - the name of the file to load the audio stream from
Returns:
true if the clip loaded successfully, false otherwise

loadClip

public static boolean loadClip(java.lang.String soundName,
                               java.net.URL url)
Loads a sound clip from a URL and gives it the specified name. This name can be used when calling the play and stop methods. The sound clip is completely loaded into memory, so it is recommended that this method be used for loading small or short sounds. For longer sounds, consider using the loadStream method.

Parameters:
soundName - the name to give this audio stream
url - the name of the file to load the audio stream from
Returns:
true if the clip loaded successfully, false otherwise

loadStream

public static boolean loadStream(java.lang.String soundName,
                                 java.lang.String filename)
Loads an audio stream from a file and gives it the specified name. This name can be used when calling the play and stop methods.

Parameters:
soundName - the name to give this audio stream
filename - the name of the file to load the audio stream from
Returns:
true if the audio stream loaded successfully, false otherwise

loadStream

public static boolean loadStream(java.lang.String soundName,
                                 java.net.URL url)
Loads an audio stream from a URL and gives it the specified name. This name can be used when calling the play and stop methods.

Parameters:
soundName - the name to give this audio stream
url - the name of the file to load the audio stream from
Returns:
true if the audio stream loaded successfully, false otherwise

play

public static void play(java.lang.String soundName,
                        boolean loop)
Plays a sound that has already been loaded by one of the sound loading methods. The sound can be played once or looped forever. If the specified sound name does not exist, this method does nothing.

Parameters:
soundName - the name of the sound to play
loop - true if the sound should loop forever, false if the sound should play once

stop

public static void stop(java.lang.String soundName)
Stops playing the specified sound.

Parameters:
soundName - the name of the sound to stop

shutdown

public static void shutdown()
Stops all playing sounds and closes all lines and audio input streams. Any previously loaded sounds will have to be re-loaded to be played again.