Package ca.cgjennings.ui
Class ClipPlayer
- java.lang.Object
-
- ca.cgjennings.ui.ClipPlayer
-
public class ClipPlayer extends java.lang.Object
A simple audio clip player intended to play audio cues as part of a user interface. The player is intentionally transparent with respect to failure; if a clip fails to load or the system does not have audio support, the player will not throw an exception. This prevents surprise failures on rare systems that do not have audio support, but it also means that these audio cues should never be the only means of providing feedback.New clip players are constructed by providing a reference to the audio file that contains the clip to play. This is either a
File
or aURL
. The constructor also specifies whether a clip should be looped. An unlooped clip will play once each timeplay()
is called. A looped clip will continue to play, repeating as necessary, until it is explicitly stopped.- Since:
- 2.1
- Author:
- Chris Jennings
-
-
Constructor Summary
Constructors Constructor Description ClipPlayer(java.io.File audioClip, boolean looped)
Create aClipPlayer
for an audio clip stored in a file.ClipPlayer(java.net.URL audioClip, boolean looped)
Create aClipPlayer
for a an audio clip stored at a URL.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
close()
Close the clip player, freeing the associated audio system resources.boolean
isClipValid()
Returnstrue
if the clip was loaded successfully and the system is able to play it.void
play()
Play the audio clip.void
stop()
If the clip is currently playing, stop playing it.
-
-
-
Constructor Detail
-
ClipPlayer
public ClipPlayer(java.io.File audioClip, boolean looped)
Create aClipPlayer
for an audio clip stored in a file. If the clip cannot be read from the file, the player will do nothing whenplay()
is called andisClipValid()
will returnfalse
.- Parameters:
audioClip
- the location of the clip to loadlooped
- whether to play the clip in a loop
-
ClipPlayer
public ClipPlayer(java.net.URL audioClip, boolean looped)
Create aClipPlayer
for a an audio clip stored at a URL. If the clip cannot be read, the player will do nothing whenplay()
is called andisClipValid()
will returnfalse
.- Parameters:
audioClip
- the location of the clip to loadlooped
- whether to play the clip in a loop
-
-
Method Detail
-
play
public void play()
Play the audio clip. If set to loop, the clip will continue to play untilstop()
is called. If the clip is invalid, nothing happens. If the clip is already playing, it will be restarted from the beginning.
-
stop
public void stop()
If the clip is currently playing, stop playing it. If the clip is not playing or is invalid, nothing happens.
-
close
public void close()
Close the clip player, freeing the associated audio system resources. Subsequent calls toplay()
will have no effect.
-
isClipValid
public boolean isClipValid()
Returnstrue
if the clip was loaded successfully and the system is able to play it.- Returns:
true
if playing the clip will produce audio
-
-