public class Music extends java.lang.Object implements Loadable
A Music track is a piece of music that can be played in the background of a CellGame. All playing and stopping of Music tracks is controlled by methods of a CellGame, so that the CellGame can keep track of the Music tracks in its music stack.
Like other Loadables, Music tracks can be manually loaded and unloaded into and out of memory. Loading may take a moment, but while a Music track is not loaded, it cannot play.
Music tracks can be played at different speeds from 0 up, with a speed of 0 pausing the Music track and a speed of 1 causing no speed change. They can also be played at different volumes between 0 and 1, with a volume of 0 making the Music track inaudible and a volume of 1 causing no volume change. Finally, the Music class has a global volume between 0 and 1, by which the effective volumes of all currently playing Music tracks are scaled. The global volume is 1 by default.
CellGame
Modifier and Type | Field and Description |
---|---|
static Music |
BLANK
A blank Music track that produces no sound and plays indefinitely.
|
Constructor and Description |
---|
Music(java.lang.String path,
boolean load)
Constructs a Music track from an audio file.
|
Music(java.lang.String path,
double loopStart,
double loopEnd,
boolean load)
Constructs a Music track from an audio file.
|
Modifier and Type | Method and Description |
---|---|
void |
fadeOut(double duration)
Instructs the music player to gradually fade this Music track's volume to
0 over the specified duration, stopping this Music track once it is
silent, if it is currently playing.
|
void |
fadeSpeed(double speed,
double duration)
Instructs the music player to gradually fade this Music track's speed to
the specified speed over the specified duration, if this Music track is
currently playing.
|
void |
fadeVolume(double volume,
double duration)
Instructs the music player to gradually fade this Music track's volume to
the specified volume over the specified duration, if this Music track is
currently playing.
|
static double |
getGlobalVolume()
Returns the global music volume.
|
double |
getPosition()
Returns the music player's position in seconds in this Music track, or 0
if it is not currently playing.
|
double |
getSpeed()
Returns the speed at which this Music track is playing, or 0 if it is not
currently playing.
|
double |
getVolume()
Returns the volume at which this Music track is playing, or 0 if it is
not currently playing.
|
boolean |
isLoaded()
Returns whether this Loadable is loaded.
|
boolean |
isLooping()
Returns whether this Music track is currently looping indefinitely.
|
boolean |
isPaused()
Returns whether this Music track is currently paused.
|
boolean |
isPlaying()
Returns whether this Music track is currently playing.
|
boolean |
load()
Loads this Loadable if it is not already loaded.
|
void |
pause()
Pauses this Music track if it is currently playing.
|
void |
resume()
Resumes playing this Music track if it currently paused.
|
static void |
setGlobalVolume(double volume)
Sets the global music volume to the specified value.
|
void |
setLooping(boolean loop)
Sets whether this Music track is looping indefinitely, if it is currently
playing.
|
void |
setPosition(double position)
Sets the music player's position in seconds in this Music track, if it is
currently playing.
|
void |
setSpeed(double speed)
Sets the speed at which this Music track is playing if it is currently
playing.
|
void |
setVolume(double volume)
Sets the volume at which this Music track is playing if it is currently
playing.
|
boolean |
unload()
Unloads this Loadable if it is currently loaded.
|
public static final Music BLANK
public Music(java.lang.String path, boolean load)
path
- The relative path to the audio fileload
- Whether this Music track should load upon creationpublic Music(java.lang.String path, double loopStart, double loopEnd, boolean load)
path
- The relative path to the audio fileloopStart
- The position in seconds in this Music track to which it
should return at the start of a looploopEnd
- The position in seconds in this Music track that, when
reached, causes it to start a new loop if it is looping. If this is
negative, this Music track will start a new loop when it reaches the end
of the track.load
- Whether this Music track should load upon creationpublic static double getGlobalVolume()
public static void setGlobalVolume(double volume)
volume
- The new global music volumepublic final boolean isLoaded()
Loadable
public final boolean load()
Loadable
public final boolean unload()
Loadable
public final boolean isPlaying()
public final boolean isPaused()
public final void pause()
public final void resume()
public final double getPosition()
public final void setPosition(double position)
position
- The music player's new position in seconds in this Music
trackpublic final double getSpeed()
public final void setSpeed(double speed)
speed
- The speed at which this Music track should playpublic final void fadeSpeed(double speed, double duration)
speed
- This Music track's eventual speedduration
- The fade's duration in secondspublic final double getVolume()
public final void setVolume(double volume)
volume
- The volume at which this Music track should playpublic final void fadeVolume(double volume, double duration)
volume
- This Music track's eventual volumeduration
- The fade's duration in secondspublic final void fadeOut(double duration)
duration
- The duration in seconds of the fade-outpublic final boolean isLooping()
public final void setLooping(boolean loop)
loop
- Whether this Music track should loop indefinitely