Jacob Davis Cherussery
Jacob Davis Cherussery

Reputation: 477

Get the total played duration of a video in MPMoviePlayer

I need to get information about total time a video is played using MPMoviePlayer.How to handle the case when user watches a 3 min video upto 2 min and moves backward to 1.30 and closes the video.The requirement is to know the fraction of video viewed by user accurately.

Upvotes: 1

Views: 410

Answers (1)

Stonz2
Stonz2

Reputation: 6396

From the Apple docs on MPMoviePlayerController:

Movie Player Notifications

A movie player generates notifications to keep your app informed about the state of movie playback. In addition to being notified when playback finishes, your app can be notified in the following situations:

  • When the movie player begins playing, is paused, or begins seeking forward or backward

Using these notifications, you could set your own timers to know the total amount of time that a video has been playing. Specifically, you probably want the MPMoviePlayerPlaybackStateDidChangeNotification.

Knowing the total percentage of the video watched could be a little trickier but still possible I think. You would need to register for the MPMediaPlayback protocol and use it in conjunction with the PlaybackStateDidChangeNotification mentioned above.

One idea I had (though probably not the best or most efficient approach) would be to create an array of BOOL values, 1 for each second of the video. When a video plays, grab the currentPlaybackTime on the player and mark off each second as it is played. If the video state changes (pause, skip forward, etc), stop marking them off until it is resumed, then start at that new index based on the new currentPlaybackTime and continue marking. When they're finished, calculate the % of indexes that have been marked.

MPMoviePlayerController
MPMediaPlayback Protocol

Let me know if this works for you!!

Upvotes: 1

Related Questions