Package com.csse3200.game.services.sound
Class BackgroundMusicService
java.lang.Object
com.csse3200.game.services.sound.BackgroundMusicService
- All Implemented Interfaces:
MusicService
A MusicService that handles playback for long background music files which are streamed from disk.
A maximum of 10 tracks can be loaded at any given time, with only one track playing.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
void
dispose()
Disposes of all loaded Music instances.boolean
isMuted()
Returns the current mute status.boolean
Checks if the given sound file is playing.void
loadSounds
(List<SoundFile> sounds) Note: Only loads first 10 songs in list.void
pause()
Pauses whichever track is currently playing.void
Attempts to pause a specific track.void
play
(BackgroundMusicType type) Begins playing loaded Music instances of the provided type.long
Plays a given sound file without the specification of a looping boolean, defaults this value to false.long
Plays a given sound file, checking whether it is valid to play as well as looping the playback if specified.void
setMuted
(boolean muted) Sets the mute status of the background music to the given boolean value.void
Attempts to stop playback for a given sound file.void
unPause()
Unpauses current music if it is paused.
-
Constructor Details
-
BackgroundMusicService
public BackgroundMusicService()
-
-
Method Details
-
play
Begins playing loaded Music instances of the provided type. Will continuously play songs at random of the given type until stop() is called. After which play will need to be called again to resume playback.- Parameters:
type
- The type of background music to be played.- Throws:
IllegalStateException
- If there are no available Music instances of the type given.
-
play
Plays a given sound file, checking whether it is valid to play as well as looping the playback if specified.- Specified by:
play
in interfaceMusicService
- Parameters:
sound
- - An enum value that implements the SoundFile interfacelooping
- - A flag to control if the sound loops- Returns:
- 0 - no play ID is returned for background music instances.
- Throws:
InvalidSoundFileException
- if the given sound file is not an instance of BackgroundMusicFile or is unplayable.
-
play
Plays a given sound file without the specification of a looping boolean, defaults this value to false.- Specified by:
play
in interfaceMusicService
- Parameters:
sound
- An enum value that implements the SoundFile interface- Returns:
- 0 - no play ID is returned for background music instances.
- Throws:
InvalidSoundFileException
- if the given sound file is not an instance of * BackgroundMusicFile or is unplayable.
-
pause
Attempts to pause a specific track. This is more important for Effects. Use pause() to pause whichever track is currently active.- Specified by:
pause
in interfaceMusicService
- Parameters:
sound
- - The SoundFile to pause.- Throws:
InvalidSoundFileException
- if the given SoundFile is not active.
-
pause
public void pause()Pauses whichever track is currently playing. -
unPause
public void unPause()Unpauses current music if it is paused. -
stop
Attempts to stop playback for a given sound file.- Specified by:
stop
in interfaceMusicService
- Parameters:
sound
- - An enum value that implements the SoundFile interface.- Throws:
InvalidSoundFileException
- if the given sound file is not playing.
-
adjustVolume
public void adjustVolume() -
isMuted
public boolean isMuted()Returns the current mute status.- Specified by:
isMuted
in interfaceMusicService
- Returns:
- the current mute status.
-
setMuted
public void setMuted(boolean muted) Sets the mute status of the background music to the given boolean value. True being muted.- Specified by:
setMuted
in interfaceMusicService
- Parameters:
muted
- - The boolean state to set
-
isPlaying
Checks if the given sound file is playing.- Specified by:
isPlaying
in interfaceMusicService
- Parameters:
sound
- - An enum value that implements the SoundFile interface- Returns:
- True if playing, false if not.
- Throws:
InvalidSoundFileException
- if the sound file provided either isn't a BackgroundSoundFile or hasn't been loaded.
-
loadSounds
Note: Only loads first 10 songs in list. Creates a private map of BackgroundSoundTypes as keys to a list of Music instances of that type.- Specified by:
loadSounds
in interfaceMusicService
- Parameters:
sounds
- - A list of SoundFiles to be loaded into memory- Throws:
InvalidSoundFileException
- if the provided list contains any non-BackgroundSoundFile instances.
-
dispose
public void dispose()Disposes of all loaded Music instances. Resets internally stored sound file maps. To resume playback after calling this, loadSounds() must be called again.- Specified by:
dispose
in interfaceMusicService
-