how to get the time duration video watched in iOS app

1.3k views Asked by At

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];
3

There are 3 answers

1
Suryakant Sharma On BEST ANSWER

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;
1
iCoder On

If u check the reference manual MPMoviePlayerController conforms to MPMediaPlayback protocol

So MPMediaPlayback protocol contains the property

@property(nonatomic) NSTimeInterval currentPlaybackTime

The current position of the playhead. (required)

Example:

MPMoviePlayerController *player = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
...

NSTimeInterval timeInterval = player.currentPlaybackTime;

Links :

MPMoviePlayerController

MPMediaPlayback

Hope this helps!

0
scollaco On

You can use Notification center:

1- On viewDidLoad:

 [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(playbackStateDidChange:)
                                          name:@"MPAVControllerPlaybackStateChangedNotification"
                                           object:nil];

2- Implement this method (seconds is an int):

- (void)playbackStateDidChange:(NSNotification *)note {
    NSLog(@"note.name=%@ state=%d", note.name, [[note.userInfo objectForKey:@"MPAVControllerNewStateParameter"] intValue]);

if ([[note.userInfo objectForKey:@"MPAVControllerNewStateParameter"] intValue] == 2) {
    timer= [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:@selector(increaseSeconds) userInfo:nil repeats:YES];
    NSLog(@"seconds: %i", seconds);
} else if([[note.userInfo objectForKey:@"MPAVControllerNewStateParameter"] intValue] == 1){
    [timer invalidate];
    NSLog(@"seconds: %i", seconds);
} else if([[note.userInfo objectForKey:@"MPAVControllerNewStateParameter"] intValue] == 0){
    NSLog(@"Total watched: %i", seconds);

    [self dismissMoviePlayerViewControllerAnimated];
}   

}

where MPAVControllerNewStateParameter == 2 (video started) MPAVControllerNewStateParameter == 1 (video stopped) MPAVControllerNewStateParameter == 0 (video finished or pressed "Done")

3- Finally implement this method:

-(void) increaseSeconds {
    seconds++;
}