Reputation: 73
I have ipad app in which i play video i want that when user plays video if it view video 2 seconds then it should show alert the time how much time user viewed video or duration
here is the code i am using for playing video.
[[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: 616
Reputation: 11607
You can record the start date and end date, then compare the date to see how long the video was played.
Date includes time too.
So:
1) When user presses play, you can go:
NSDate *startPlayDate = [NSDate date];
2) When user stops the video, you can go:
NSDate *stopPlayDate = [NSDate date];
3) Now compare the difference in the two dates, you should see how many seconds the user played the video for:
NSCalendar *gregorianCalendar = [[NSCalendar alloc] initWithCalendarIdentifier: NSGregorianCalendar];
NSDateComponents* components = [gregorianCalendar components: NSHourCalendarUnit | NSMinuteCalendarUnit | NSSecondCalendarUnit
fromDate:startPlayDate
toDate:stopPlayDate
options:0] ;
NSString *message = [[NSString alloc] initWithFormat:@"Video played for: %d hours, %d minutes, %d seconds", components.hour, components.minute, components.second];
UIAlert *alert = [[UIAlertView alloc] initWithTitle:@"Duration"
message:message
delegate:self
cancelButton:@"OK"
otherButton:nil, nil];
[alert show];
Is that what you were looking for?
Upvotes: 0
Reputation: 2431
Have a look at the MPMediaPlayback Protocol.
currentPlaybackTime: For content streamed live from a server, this value represents the time from the beginning of the playlist when it was first loaded.
Upvotes: 0
Reputation: 2314
MPMoviePlayerController generates notifications to keep your app informed about the state of movie playback.
1, When the movie player begins playing, is paused, or begins seeking forward or backward 2, When AirPlay playback starts or ends. For details please click here
Use the notifications and gather the currentPlaybackTime property, and the video duration property for further calculations.
- (void)MPMoviePlayerLoadStateDidChange:(NSNotification *)notification
{
if ((player.loadState & MPMovieLoadStatePlaythroughOK) == MPMovieLoadStatePlaythroughOK)
{
NSLog(@"content play length is %g seconds", player.duration);
}
}
Upvotes: 1
Reputation: 7226
The currentPlaybackTime
property in the MPMediaPlayback
protocol gives you that info.
Have a look at the currentPlaybackTime
property of the MPMediaPlayback
protocol. MPMoviePlayerController
adheres to that protocol, hence you can use it directly on any instance of that class.
MPMoviePlayerController *player = [...];
[...]
NSLog(@"current time: %g", player.currentPlaybackTime);
From the MPMediaPlayback Reference;
currentPlaybackTime The current position of the playhead.
@property(nonatomic) NSTimeInterval currentPlaybackTime
Upvotes: 0