Save the video position and resume later

You can save the current playback position in a video and resume the user at the same position in a future session.

Dynamically inserted ads differ between user sessions, so saving the position with spliced ads will refer to a different position in a future session. The PSDK provides methods to retrieve the playback position, ignoring spliced ads. Your application can then seek to a specific position while ignoring spliced ads.

  1. When the user quits a video, retrieve and save the position in the video (excluding duration of ads).

    Ad breaks can vary in each session due to ad patterns, frequency capping, and so on. Therefore, the current time of the video in one session could be different in a future session. When saving a position in the video, retrieve the local time (which excludes the duration of all ads up to that position), which you can save either on the device or in a database on the server.

    For example, if the user is at the 20th minute of the video and this includes five minutes of ads, current time will be 1200 seconds, while local time at this position will be 900.

    Note: Local time and current time are the same for live/linear streams. In this case, convertToLocalTime has no effect. For VOD, local time remains unchanged while ads play.
    // Save the user session when player activity stops
        @Override
        public void onStop(){
            super.onStop();
     ...
            prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
            SharedPreferences.Editor editor = prefs.edit();
            editor.putLong(LAST_LOCAL_TIME, _mediaPlayer.getLocalTime());                   // get the local time where stream stopped playing and save it in System preferences
            editor.putString(LAST_MEDIA_RESOURCE, _contentInfo.toMediaResource().getUrl());
            editor.commit();
     ...
        }
  2. Restore the user session when player activity resumes.
        @Override
        public void onResume() {
            super.onResume();
     ...
            prefs = PreferenceManager.getDefaultSharedPreferences(getActivity().getApplicationContext());
            if(prefs.getString(LAST_MEDIA_RESOURCE, "nil").equals(_contentInfo.toMediaResource().getUrl())){
                _lastKnownLocalTime = prefs.getLong(LAST_LOCAL_TIME, 0);    // get the last local time saved in system preferences
                if(_lastKnownLocalTime > 0){
                     _shouldResumePlayback = true;
                }
            }
     ...
        }
  3. Resume the user at the same position. To resume the user to the same position saved from a previous session, use seekToLocalTime. Call this method only with local time values. Incorrectly calling it with the current time results in incorrect behavior. (To seek to the current time, use seek. ) When your application receives the onStateChanged status change event, seek to the saved local time.
  4. Provide the ad breaks to present to the user through the ad policy selector interface.

    Implement a custom ad policy selector (by extending the default ad policy selector). Provide the ad breaks that must be presented to the user by implementing selectAdBreaksToPlay. This method includes a pre-roll ad break and the mid-roll ad breaks before the local time position. Your application can decide to play a pre-roll ad break and resume to the specified local time, play a mid-roll ad break and resume to the specified local time, or play no ad breaks.