Thursday, April 7, 2011

How to Reset MPMoviePlayer Timeline

When you stream a video (HTTP Live Streaming) over network till finish, the timeline might show there's still a few seconds missing. The problem is that the initial video duration is an estimation, not the exact video length.

There is a system callback, which is called when real video duration is known:
MPMovieDurationAvailableNotification (iOS 3.2+)
This notification is posted when the duration of a movie object is determined. The object of the notification is the MPMoviePlayerController object itself. There is no userInfo dictionary. The duration value is reflected in the duration property of the movie player controller.
However I couldn't make it work, even thought that would be the obvious place. Trying to reset player.endPlaybackTime at MPMovieDurationAvailableNotification callback didn't change anything on UI. There was still a few seconds of video "missing" when playback ended.

Since I couldn't change values at runtime, the next best thing is to do cleanup at finish (and hope that the user didn't pay attention to total playtime):
[[NSNotificationCenter defaultCenter] addObserver:self
    selector:@selector(playerFinished:)
    name:MPMoviePlayerPlaybackDidFinishNotification
    object:self.moviePlayer];

- (void)playerFinished:(NSNotification *)aNotification
{
    MPMoviePlayerController *player =
      [aNotification object];
    // --- No effect
    //player.endPlaybackTime =
    //   player.currentPlaybackTime;
    // --- Restarts video
    //player.currentPlaybackTime =
    //   player.endPlaybackTime;
    // --- This works
    player.currentPlaybackTime =
      player.duration;

}

No comments:

Post a Comment