I set up the movie player like this:
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:@"whatever.mp4"];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
self.moviePlayer.shouldAutoplay = YES;
[self.moviePlayer prepareToPlay];
self.moviePlayer.repeatMode = MPMovieRepeatModeOne;
self.moviePlayer.view.frame = self.container.bounds;
self.moviePlayer.view.userInteractionEnabled = NO;
[self.container addSubview:self.moviePlayer.view];
[[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(moviePlayBackDidFinish:) name: MPMoviePlayerPlaybackStateDidChangeNotification
object: self.moviePlayer];
The notification is necessary to keep the player looping, since repeatMode is pretty much useless (the video will repeat once or twice, maybe a few times depending on the price of rice in China, then stop). So to keep the video looping you have to do this:
- (void)moviePlayBackDidFinish:(NSNotification *)note {
if (note.object == self.moviePlayer) {
NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded) {
[self.moviePlayer play];
}
}
}
Now the problem is that I need to be able to pause the video. For some reason a call to
[self.moviePlayer pause];
results in the notification getting fired with reason == MPMovieFinishReasonPlaybackEnded even though the documentation clearly states this:
Constants
MPMovieFinishReasonPlaybackEnded
The end of the movie was reached.
Available in iOS 3.2 and later.
Declared in MPMoviePlayerController.h.
The end of the movie was not reached. I just called pause:. So the notification gets fired and thus the movie gets played again, negating the pause action.
So you can see the problem. How can I successfully pause a looping video?
create a BOOL in your class and initialize it to NO,
@property (nonatomic, assign) BOOL isTriggeredByPause;
before calling
[self.moviePlayer pause];
set its value,
self.isTriggeredByPause = YES;
in your method check for it,
(void)moviePlayBackDidFinish:(NSNotification *)note {
if (note.object == self.moviePlayer) {
NSInteger reason = [[note.userInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey] integerValue];
if (reason == MPMovieFinishReasonPlaybackEnded) {
if(!self.isTriggeredByPause)
{
[self.moviePlayer play];
}
}
}
}
Modify value of self.isTriggeredByPause on manual play too, or loop won't work.