Search code examples
iosiphonempmediaplayercontroller

how to get the time duration video watched in iOS app


I am making an iphone app in which playing video from url server it works fine but i want lets say video is for 3 minutes or 4 minutes how much time user viewed video like it played video for 1 minuted and stoped likewise.

NSURL *url = [NSURL URLWithString:newString];
NSLog(@"New File name is %@",newString);            
mp = [[MPMoviePlayerViewController alloc] initWithContentURL:url];
[[mp moviePlayer] prepareToPlay];
[[mp moviePlayer] setUseApplicationAudioSession:NO];
[[mp moviePlayer] setShouldAutoplay:YES];
[[mp moviePlayer] setControlStyle:2];
//[[mp moviePlayer] setRepeatMode:MPMovieRepeatModeOne];
[self presentMoviePlayerViewControllerAnimated:mp];

Solution

  • I think, you can start NSTimer at the time of presenting MPMoviePlayerViewController and listen the notification MPMoviePlayerPlaybackDidFinishNotification or MPMoviePlayerPlaybackDidFinishReasonUserInfoKey and calculate the time.

    EDIT

    Best way is to access MPMediaPlayback's currentPlaybackTime property using MPMoviePlayerPlaybackDidFinishNotification notification

    this will give you the actual time. In your case you can access this property as

      NSTimeInterval time =  mp.moviePlayer.currentPlaybackTime;