Class PlaybackStateMachine

java.lang.Object
com.castlabs.android.player.state.PlaybackStateMachine

public final class PlaybackStateMachine extends Object
Turns player events into changes in the shared playback state model.

Command and lifecycle methods describe work accepted by PlayerController. Playback methods include a session ID so callbacks from an old player can be ignored. The dispatcher handles every input in order before this class reads or changes its state.

  • Constructor Details

    • PlaybackStateMachine

      public PlaybackStateMachine(@NonNull Handler handler, @NonNull PlaybackStateListener listener)
      Creates a state machine.
      Parameters:
      handler - handler used to process state changes and call the listener
      listener - listener called for each state change
  • Method Details

    • getState

      @NonNull public PlaybackState getState()
      Returns the current shared playback state.
      Returns:
      current state
    • onOpenAccepted

      public void onOpenAccepted(int sessionId, boolean autoplay)
      Starts a new playback-state session after the controller accepts an open call.
      Parameters:
      sessionId - ID used to reject callbacks from an older player
      autoplay - whether the open call requested autoplay
    • onTrackModelReady

      public void onTrackModelReady()
      Reports that the first usable track model for the current open is ready.
    • onPlayRequested

      public void onPlayRequested()
      Reports that the controller accepted a request to start playback.
    • onPauseRequested

      public void onPauseRequested()
      Reports that the controller accepted a request to pause playback.
    • onSeekAccepted

      public void onSeekAccepted(long currentPositionMs, long targetPositionMs, long durationMs, long bufferedPositionMs, long backBufferDurationMs, boolean deferUntilEnded, boolean internalSeekToEnd)
      Saves a seek when the controller accepts it.

      The position, duration, and buffer values are taken before the player handles the seek. They are used later to decide whether loading was needed and to build seek event data.

      Parameters:
      currentPositionMs - content position before the seek
      targetPositionMs - requested content position
      durationMs - content duration, or a negative value when unknown
      bufferedPositionMs - end of the buffered content, or a negative value when unknown
      backBufferDurationMs - amount of buffered content kept before the current position
      deferUntilEnded - whether the previous playback must enter Ended first
      internalSeekToEnd - whether the controller made this seek as part of its end handling
    • onDataUnavailable

      public void onDataUnavailable(int sessionId, boolean loading, long currentPositionMs, long bufferedPositionMs, @Nullable PlaybackStateTransitionReason adReason)
      Reports that playback needs data.
      Parameters:
      sessionId - ID of the player that sent this update
      loading - whether the player is currently loading data
      currentPositionMs - latest content position
      bufferedPositionMs - latest end of the buffered content
      adReason - ad start or stop reason, or null
    • onDataAvailable

      public void onDataAvailable(int sessionId, @Nullable PlaybackStateTransitionReason adReason)
      Reports that the player has enough data for the current position.
      Parameters:
      sessionId - ID of the player that sent this update
      adReason - ad start or stop reason from the same callback batch, or null
    • onPlaybackTemporarilySuppressed

      public void onPlaybackTemporarilySuppressed(int sessionId, @Nullable PlaybackStateTransitionReason adReason)
      Reports that playback is temporarily blocked and will be retried automatically.
      Parameters:
      sessionId - ID of the player that sent this update
      adReason - ad start or stop reason from the same callback batch, or null
    • onPlaybackStoppedWithoutResume

      public void onPlaybackStoppedWithoutResume(int sessionId, @Nullable PlaybackStateTransitionReason adReason)
      Reports that playback is not running and will not restart automatically.
      Parameters:
      sessionId - ID of the player that sent this update
      adReason - ad start or stop reason from the same callback batch, or null
    • onPlayingObserved

      public void onPlayingObserved(int sessionId, @Nullable PlaybackStateTransitionReason adReason)
      Reports that the player is or was actually playing in the current callback batch.
      Parameters:
      sessionId - ID of the player that sent this update
      adReason - ad start or stop reason from the same callback batch, or null
    • onContentEnded

      public void onContentEnded(int sessionId, long currentPositionMs, long durationMs)
      Reports that the content, rather than an ad, reached its end.
      Parameters:
      sessionId - ID of the player that sent this update
      currentPositionMs - content position when the end was reported
      durationMs - content duration, or a negative value when unknown
    • onReleaseStarted

      public void onReleaseStarted()
      Reports that release has started and moves the state to Stopping.
    • onReleaseCompleted

      public void onReleaseCompleted()
      Reports that release has finished, moves the state to Idle, and clears the session.
    • onFatalError

      public void onFatalError()
      Reports a fatal error and moves the state to Error.