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). Use the localTime property to read this 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, currentTime will be 1200 seconds, while localTime 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.
    - (void) onMediaPlayerTimeChange:(NSNotification *)notification
    {
        CMTimeRange seekableRange = self.player.seekableRange;
        
        if (CMTIMERANGE_IS_VALID(seekableRange))
        {
            double seekableRangeStart = CMTimeGetSeconds(seekableRange.start);
            double seekableRangeDuration = CMTimeGetSeconds(seekableRange.duration);
            double currentTime = CMTimeGetSeconds(self.player.currentTime); // includes ads
            double localTime = CMTimeGetSeconds(self.player.localTime); // no ads
        }
    }
  2. 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 seekToTime. ) When your application receives the PTMediaPlayerStatusReady status change event, seek to the saved local time.
    [self.player seekToLocalTime:CMTimeMake(900, 1) completionHandler:^(BOOL finished) {
                    [self.player play];
                }];
  3. 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.