Display a seek scrub bar with the current playback time position

The PSDK supports seeking to a specific position (time) in live streams where the stream is either a sliding-window playlist or an event type and in VOD.

You can seek to a particular location in the stream.

Note:

Seeking in a live stream is allowed only for DVR.

  1. Wait for the PSDK to be in a valid state for seeking.

    Valid states are PREPARED, COMPLETE, PAUSED, and PLAYING. Being in a valid state ensures that the media resource has successfully loaded. If the player is not in a valid seekable state, attempting to call the following methods throws an IllegalStateException.

    For example, wait for the PSDK to call your PlaybackEventListener.onPrepared callback.

  2. Pass the requested seek position to the MediaPlayer.seek method as a parameter expressed in milliseconds.

    This moves the play head to a different position in the stream. Note that the requested seek position might not coincide with the actual computed position.

    void seek(long position) throws IllegalStateException;
    
  3. Wait for the PSDK to call the QOSEventListener.onSeekComplete callback, which returns the adjusted position in the callback's position parameter.

    This is important because the actual start position after the seek could be different than the requested position. Various rules might apply, including:

    • If you seek into the middle of an ad break, the seek position is adjusted to the beginning of the ad break.
    • If you seek forward past one or more ad breaks, the playhead is positioned at the beginning of the last ad break previous to the specified seek position.
    • You can seek only in the asset’s seekable duration. For video on demand, that is from 0 through the asset's duration.
  4. Use the native SeekBar to set an OnSeekBarChangeListener to see when the user is scrubbing.
    In the following example, the user scrubs the seek bar to seek to the desired position.
    seekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
    @Override
    public void onProgressChanged(SeekBar seekBar, int progress, boolean isFromUser) {
    if (isFromUser) {	
         // Update the seek bar thumb, with the position provided by the user.
        setPosition(progress);
    }
    }
    @Override
    public void onStartTrackingTouch(SeekBar seekBar) {
    isSeeking = true;
    }
    @Override
    public void onStopTrackingTouch(SeekBar seekBar) {
    isSeeking = false;
    // Retrieve the playback range.
    TimeRange playbackRange = mediaPlayer.getPlaybackRange();
    // Make sure to seek inside the playback range.
    long seekPosition = Math.min(Math.round(seekBar.getProgress()), playbackRange.getDuration());
    // Perform seek.
    seek(playbackRange.getBegin() + seekPosition);
    }
    });
    
  5. Set up event listener callbacks for changes in the user’s seek activity.

    The seek operation is asynchronous, so the PSDK dispatches these events related to seeking:

    • QOSEventListener.onSeekStart - Calledto indicate that seek is starting.
    • QOSEventListener.onSeekComplete - Called to indicate that seeking was successful.
    • QOSEventListener.onOperationFailed - Called to indicate that seeking was unsuccessful.

    The media player might readjust the seek position provided by the user. Therefore, check the QOSEventListener.onSeekComplete callback, which returns the adjusted seek position.

  6. Listen for the QOSEventListener.onOperationFailed event, which indicates that the seek operation failed for various reasons, and take appropriate actions.

    QOSEventListener.onOperationFailed passes the appropriate warning. Your application needs to decide how to proceed in this case. For instance, it could try to seek again or it could continue playback from the previous position.