Reputation: 57
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];
Upvotes: 0
Views: 1365
Reputation: 3960
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;
Upvotes: 1
Reputation: 1645
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 :
Hope this helps!
Upvotes: 0
Reputation: 922
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++;
}
Upvotes: 0