|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.ObjectAudioPlayer
public class AudioPlayer
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
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
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 |
---|
public AudioPlayer()
Method Detail |
---|
public static boolean loadClip(java.lang.String soundName, java.lang.String filename)
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.
soundName
- the name to give this audio streamfilename
- the name of the file to load the audio stream from
true
if the clip loaded successfully,
false
otherwisepublic static boolean loadClip(java.lang.String soundName, java.net.URL url)
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.
soundName
- the name to give this audio streamurl
- the name of the file to load the audio stream from
true
if the clip loaded successfully,
false
otherwisepublic static boolean loadStream(java.lang.String soundName, java.lang.String filename)
play
and stop
methods.
soundName
- the name to give this audio streamfilename
- the name of the file to load the audio stream from
true
if the audio stream loaded successfully,
false
otherwisepublic static boolean loadStream(java.lang.String soundName, java.net.URL url)
URL
and gives it the specified name. This name can be used when calling
the play
and stop
methods.
soundName
- the name to give this audio streamurl
- the name of the file to load the audio stream from
true
if the audio stream loaded successfully,
false
otherwisepublic static void play(java.lang.String soundName, boolean loop)
soundName
- the name of the sound to playloop
- true
if the sound should loop forever,
false
if the sound should play oncepublic static void stop(java.lang.String soundName)
soundName
- the name of the sound to stoppublic static void shutdown()
|
||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |