Search code examples
iphoneipadios4videompmovieplayercontroller

How to play a local mp4 video in iphone from a saved time interval programmatically


I have a scenario in which I want user to be able to start a video from the point he left previously. I am currently using MPMoviePlayerController. I am able to store playback duration using MPMoviePlayerController's notifications.

So what I need is whenever user is starting same video he has an option to start it from the duration he left. Is it possible? and if it is how can I do that. Should I use MPMoviePlayerController, since I have looked in all methods in documentation and did not find anything. Below is my current code to play video.

- (void) playDownloadedFile:(NSString*) filePath
{
    MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:filePath]];

    [[NSNotificationCenter defaultCenter] 
     addObserver:self
     selector:@selector(movieFinishedCallback:)
     name:MPMoviePlayerPlaybackDidFinishNotification
     object:player];

    [[NSNotificationCenter defaultCenter] 
     addObserver:self
     selector:@selector(movieDurationAvailableCallback:)
     name:MPMovieDurationAvailableNotification
     object:player];

    [[NSNotificationCenter defaultCenter] 
     addObserver:self
     selector:@selector(moviePlaybackStateDidChangeCallback:)
     name:MPMoviePlayerPlaybackStateDidChangeNotification
     object:player];

    player.view.frame = CGRectMake(50, 25, 924, 718);
    [self.view addSubview:player.view];

    //---play movie---
    [player play];
    videoState = [[VideoState alloc] init];
}

- (void) movieDurationAvailableCallback:(NSNotification*) aNotification {
    MPMoviePlayerController *moviePlayer = [aNotification object];
    videoState.videoDuration = moviePlayer.duration;
}

- (void) moviePlaybackStateDidChangeCallback:(NSNotification*) aNotification {
    MPMoviePlayerController *moviePlayer = [aNotification object];
    videoState.playBackState = moviePlayer.playbackState;
}

Solution

  • MPMoviePlayerController has a property called initialPlaybackTime. Set it to start where the viewer left off.