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 TypeMethodDescriptionvoidvoiddispose()Disposes of all loaded Music instances.booleanisMuted()Returns the current mute status.booleanChecks if the given sound file is playing.voidloadSounds(List<SoundFile> sounds) Note: Only loads first 10 songs in list.voidpause()Pauses whichever track is currently playing.voidAttempts to pause a specific track.voidplay(BackgroundMusicType type) Begins playing loaded Music instances of the provided type.longPlays a given sound file without the specification of a looping boolean, defaults this value to false.longPlays a given sound file, checking whether it is valid to play as well as looping the playback if specified.voidsetMuted(boolean muted) Sets the mute status of the background music to the given boolean value.voidAttempts to stop playback for a given sound file.voidunPause()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:
playin 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:
playin 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:
pausein 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:
stopin interfaceMusicService- Parameters:
sound- - An enum value that implements the SoundFile interface.- Throws:
InvalidSoundFileException- if the given sound file is not playing.
-
setMuted
public void setMuted(boolean muted) Sets the mute status of the background music to the given boolean value. True being muted.- Specified by:
setMutedin interfaceMusicService- Parameters:
muted- - The boolean state to set
-
adjustVolume
public void adjustVolume() -
isMuted
public boolean isMuted()Returns the current mute status.- Specified by:
isMutedin interfaceMusicService- Returns:
- the current mute status.
-
isPlaying
Checks if the given sound file is playing.- Specified by:
isPlayingin 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:
loadSoundsin 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:
disposein interfaceMusicService
-