Class BackgroundMusicService

java.lang.Object
com.csse3200.game.services.sound.BackgroundMusicService
All Implemented Interfaces:
MusicService

public class BackgroundMusicService extends Object implements 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
    Constructor
    Description
     
  • Method Summary

    Modifier and Type
    Method
    Description
    void
     
    void
    Disposes of all loaded Music instances.
    boolean
    Returns the current mute status.
    boolean
    Checks if the given sound file is playing.
    void
    Note: Only loads first 10 songs in list.
    void
    Pauses whichever track is currently playing.
    void
    Attempts to pause a specific track.
    void
    Begins playing loaded Music instances of the provided type.
    long
    play(SoundFile sound)
    Plays a given sound file without the specification of a looping boolean, defaults this value to false.
    long
    play(SoundFile sound, boolean looping)
    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
    stop(SoundFile sound)
    Attempts to stop playback for a given sound file.
    void
    Unpauses current music if it is paused.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • BackgroundMusicService

      public BackgroundMusicService()
  • Method Details

    • play

      public void play(BackgroundMusicType type) throws IllegalStateException
      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

      public long play(SoundFile sound, boolean looping) throws InvalidSoundFileException
      Plays a given sound file, checking whether it is valid to play as well as looping the playback if specified.
      Specified by:
      play in interface MusicService
      Parameters:
      sound - - An enum value that implements the SoundFile interface
      looping - - 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

      public long play(SoundFile sound) throws InvalidSoundFileException
      Plays a given sound file without the specification of a looping boolean, defaults this value to false.
      Specified by:
      play in interface MusicService
      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

      public void pause(SoundFile sound) throws InvalidSoundFileException
      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 interface MusicService
      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

      public void stop(SoundFile sound) throws InvalidSoundFileException
      Attempts to stop playback for a given sound file.
      Specified by:
      stop in interface MusicService
      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 interface MusicService
      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 interface MusicService
      Parameters:
      muted - - The boolean state to set
    • isPlaying

      public boolean isPlaying(SoundFile sound) throws InvalidSoundFileException
      Checks if the given sound file is playing.
      Specified by:
      isPlaying in interface MusicService
      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

      public void loadSounds(List<SoundFile> sounds) throws InvalidSoundFileException
      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 interface MusicService
      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 interface MusicService