Package greenfoot
Class GreenfootSound
java.lang.Object
greenfoot.GreenfootSound
Represents audio that can be played in Greenfoot. A GreenfootSound loads the audio from a file.
The sound cannot be played several times simultaneously, but can be played several times sequentially.
Most files of the following formats are supported: AIFF, AU, WAV, MP3 and MIDI.
-
Constructor Summary
-
Method Summary
Modifier and TypeMethodDescriptionint
Get the current volume of the sound, between 0 (off) and 100 (loudest.)boolean
True if the sound is currently playing.void
pause()
Pauses the current sound if it is currently playing.void
play()
Start playing this sound.void
playLoop()
Play this sound repeatedly in a loop.void
setVolume
(int level) Set the current volume of the sound between 0 (off) and 100 (loudest.)void
stop()
Stop playing this sound if it is currently playing.toString()
Returns a string representation of this sound containing the name of the file and whether it is currently playing or not.
-
Constructor Details
-
GreenfootSound
Creates a new sound from the given file.- Parameters:
filename
- Typically the name of a file in the sounds directory in the project directory.
-
-
Method Details
-
play
public void play()Start playing this sound. If it is playing already, it will do nothing. If the sound is currently looping, it will finish the current loop and stop. If the sound is currently paused, it will resume playback from the point where it was paused. The sound will be played once. -
playLoop
public void playLoop()Play this sound repeatedly in a loop. If called on an already looping sound, it will do nothing. If the sound is already playing once, it will start looping instead. If the sound is currently paused, it will resume playing from the point where it was paused. -
stop
public void stop()Stop playing this sound if it is currently playing. If the sound is played again later, it will start playing from the beginning. If the sound is currently paused it will now be stopped instead. -
pause
public void pause()Pauses the current sound if it is currently playing. If the sound is played again later, it will resume from the point where it was paused.Make sure that this is really the method you want. If possible, you should always use
stop()
, because the resources can be released after callingstop()
. The resources for the sound will not be released while it is paused.- See Also:
-
isPlaying
public boolean isPlaying()True if the sound is currently playing.- Returns:
- Whether the sound is currently playing.
-
getVolume
public int getVolume()Get the current volume of the sound, between 0 (off) and 100 (loudest.)- Returns:
- A number between 0-100 represents the current sound volume.
-
setVolume
public void setVolume(int level) Set the current volume of the sound between 0 (off) and 100 (loudest.)- Parameters:
level
- the level to set the sound volume to.
-
toString
Returns a string representation of this sound containing the name of the file and whether it is currently playing or not.
-