Class JukeBox

java.lang.Object
de.pottgames.tuningfork.jukebox.JukeBox

public class JukeBox extends Object
A music player class playing Songs from a PlayList. It's called JukeBox to avoid confusion with how libgdx uses the word 'Music'.
  • Field Details

    • observer

      protected final com.badlogic.gdx.utils.Array<JukeBoxObserver> observer
    • currentPlayList

      protected PlayList currentPlayList
    • playListProvider

      protected final PlayListProvider playListProvider
    • currentSong

      protected Song currentSong
    • stopped

      protected boolean stopped
    • volume

      protected float volume
    • softStop

      protected boolean softStop
    • softStopStartTime

      protected long softStopStartTime
    • softStopFadeDuration

      protected float softStopFadeDuration
    • softStopFadeCurve

      protected com.badlogic.gdx.math.Interpolation softStopFadeCurve
    • softStopFadeStartVolume

      protected float softStopFadeStartVolume
    • eventPool

      protected com.badlogic.gdx.utils.Pool<JukeBoxEvent> eventPool
    • eventHistory

      protected com.badlogic.gdx.utils.Array<JukeBoxEvent> eventHistory
  • Constructor Details

    • JukeBox

      public JukeBox(PlayListProvider playListProvider)
      Creates a new JukeBox.
      Parameters:
      playListProvider - the playlist provider
  • Method Details

    • update

      public void update()
      Updates the JukeBox. This method should be called every frame.
    • softStopFade

      protected void softStopFade(SongSource source)
    • determineFadeVolume

      protected float determineFadeVolume(SongSource source, SongSettings settings)
    • fadeIn

      protected float fadeIn(SongSource source, SongSettings settings, float playbackPos)
    • fadeOut

      protected float fadeOut(SongSource source, SongSettings settings, float playbackPos, float songDuration)
    • setVolume

      public void setVolume(float volume)
      Sets the master volume of the Jukebox.
      The change isn't applied immediately, it will be applied in the next call to update().
      The final volume equation looks like:
      master volume = JukeBox.getVolume() * SongSettings.fadeVolume()
      Parameters:
      volume - in the range of 0.0 - 1.0 with 0 being silent and 1 being the maximum volume, values outside of the range will be clamped
    • getVolume

      public float getVolume()
      Returns the JukeBox's master volume.
      The final volume equation looks like:
      master volume = JukeBox.getVolume() * SongSettings.fadeVolume()
      Returns:
      the master volume in the range of 0.0 - 1.0 with 0 being silent and 1 being the maximum volume
    • play

      public void play()
      Starts playback.
    • pause

      public void pause()
      Pauses playback. Calling play() will resume playback.
    • stop

      public void stop()
      Stops playback and resets the current PlayList if applicable.
    • resetSoftStop

      protected void resetSoftStop(boolean clearResume)
    • softStop

      public boolean softStop(com.badlogic.gdx.math.Interpolation fadeOutCurve, float fadeOutDuration)
      Fades out the currently playing song and stops the JukeBox afterwards.

      There's a couple of reasons why the JukeBox might stop early:
      - the rest of the song is shorter than the desired fadeOutDuration
      - the song duration is not available
      - fadeOutCurve is null
      - fadeOutDuration is smaller or equal 0
      Parameters:
      fadeOutCurve - the interpolation used to fade-out
      fadeOutDuration - fade out duration in seconds
      Returns:
      false if a soft stop couldn't be performed and it is stopped right away
    • softStopAndResume

      public void softStopAndResume(com.badlogic.gdx.math.Interpolation fadeOutCurve, float fadeOutDuration)
      Soft stops (see softStop(Interpolation, float)), ends the current PlayList and resumes playback afterward if possible.

      Since it may not be clear at first glance what the use cases are, here is an example:
      The World-PlayList is running, but the player gets ambushed, whereupon the World-PlayList should fade-out and the Danger-PlayList should begin playing.
      This is achievable in 2 steps:
      1. change the theme in ThemePlayListProvider ThemePlayListProvider.setTheme(int)
      2. call softStopAndResume on the JukeBox
      Parameters:
      fadeOutCurve - the interpolation used to fade-out
      fadeOutDuration - the fade-out duration in seconds
    • clear

      public void clear()
      Immediately stops playback, removes all observers and sets the JukeBox into a stopped state.
    • getCurrentSong

      public Song getCurrentSong()
      Returns the Song that is currently playing. May be null.
      Returns:
      the song
    • isPlaying

      public boolean isPlaying()
      Returns true if this JukeBox is playing at the moment.
      Returns:
      true if playing
    • nextSong

      protected void nextSong()
    • addObserver

      public void addObserver(JukeBoxObserver observer)
      Adds an observer. See JukeBoxObserver for details.
      Parameters:
      observer - the jukebox observer
    • removeObserver

      public void removeObserver(JukeBoxObserver observer)
      Removes an observer.
      Parameters:
      observer - the jukebox observer
    • pushEvent

      protected void pushEvent(JukeBoxEvent.JukeBoxEventType type)
    • pushEvent

      protected void pushEvent(JukeBoxEvent.JukeBoxEventType type, Song song)
    • pushEvent

      protected void pushEvent(JukeBoxEvent.JukeBoxEventType type, PlayList playList)
    • handleEvents

      protected void handleEvents()
    • notifySongStart

      protected void notifySongStart(Song song)
    • notifySongEnd

      protected void notifySongEnd(Song song)
    • notifyPlayListStart

      protected void notifyPlayListStart(PlayList playList)
    • notifyPlayListEnd

      protected void notifyPlayListEnd(PlayList playList)
    • notifyJukeBoxEnd

      protected void notifyJukeBoxEnd()
    • notifyJukeBoxStart

      protected void notifyJukeBoxStart()
    • notifyJukeBoxPause

      protected void notifyJukeBoxPause()