How do you determine a user's current location within a MPMoviePlayer instance?
-
20-09-2019 - |
Question
I have a long movie that I'll be putting in an app, and I wanted to know what the best way to put the user back to the point they left off. Can someone point me in the right direction?
So if the user is watching the film, hit done, is there a notification of the current time or something that I can store and load the instance again with that time?
Thanks!
Solution
I've found an OS 3.0 technique that doesn't make use of private API components.
You can register to receive MPAVControllerTimeDidJumpNotification notifications and grab the MPAVControllerTimeParameter NSNumber out of that notification's userInfo Dictionary.
For example, just before you start playback register to receive the notifications:
#define MPAVControllerTimeDidJumpNotification @"MPAVControllerTimeDidJumpNotification"
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(handleTimeChanged:) name:MPAVControllerTimeDidJumpNotification object:nil];
Then start the movie playing. Add a method that will be called for each time change as the movie plays:
-(void)handleTimeChanged:(NSNotification *)notification
{
NSDictionary * userInfo = notification.userInfo;
int lastPositionInSeconds = [[userInfo valueForKey:@"MPAVControllerTimeParameter"] intValue];
NSLog( @"Last time was %d", lastPositionInSeconds );
}
And when the movie stops playing (you know this by listening for MPMoviePlayerPlaybackDidFinishNotification notifications) stop listening for the MPAVControllerTimeDidJumpNotification notifications.
OTHER TIPS
This question duplicates or is related to the following other questions here:
- chapters in videos for the iPhone
- How to jump to certain time with the MPMoviePlayer of iPhoneSDK?
- MPMoviePlayerController movie length
To reiterate the answers from those questions, there is no current public API to start a movie at a specific frame or point in time. While there are private APIs available (-currentTime
and -setCurrentTime
), they are subject to change and will get your app rejected during the approval process.
`NSDictionary * userInfo = notification.userInfo;
int lastPositionInSeconds = [[userInfo valueForKey:@"MPAVControllerTimeParameter"] intValue];
`// saving an NSInteger
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setInteger:lastPositionInSeconds forKey:@"currentMovieTime"];
// getting an NSInteger
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSInteger myInt = [prefs integerForKey:@"currentMovieTime"]; // default is zero if nil
currentMovieTime = myInt;
self.streamMoviePlayer.initialPlaybackTime = currentMovieTime;
You can use this to set/get the variable and use that to start the movie at the right time. This will work even if the user restarts their phone or restarts the application.
Slava,
I actually watched for all events getting fired by the system during movie playback and didn't see anything else that might help. That MPAVControllerTimeDidJumpNotification does get fired periodically during playback, but not as often as, say, every second. It may get fired for each keyframe, but I'm not sure about that.