xcode MPMoviePlayerPlaybackDidFinishNotification 在电影播放器上按下“DONE”时不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5186324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
MPMoviePlayerPlaybackDidFinishNotification not working when 'DONE' is pressed on movie player
提问by Entekis
I've been trying to figure this out and tried to follow any advice out there but I can't seem to get 'MPMoviePlayerPlaybackDidFinishNotification' to work after the user presses 'Done' on the movie player.
我一直在试图解决这个问题并尝试遵循任何建议,但在用户按下电影播放器上的“完成”后,我似乎无法让“MPMoviePlayerPlaybackDidFinishNotification”工作。
- (void)myMovieFinishedCallback:(NSNotification*)aNotification {
MPMoviePlayerController* theMovie=[aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
[theMovie pause];
[theMovie stop];
[theMovie autorelease];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
}
- (void)myMovieViewFinishedCallback:(NSNotification*)aNotification {
MPMoviePlayerViewController* theMovieView=[aNotification object];
[self dismissMoviePlayerViewControllerAnimated];
[[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovieView];
[theMovieView pause];
[theMovieView stop];
[theMovieView autorelease];
[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
}
- (IBAction)safetyVideo:(id)sender {
NSString *path = [[NSBundle mainBundle] pathForResource:@"Ball_Crunch" ofType:@"m4v"];
if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2) {
MPMoviePlayerViewController*tmpMoviePlayViewController=[[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]] retain];
if (tmpMoviePlayViewController) {
[self presentMoviePlayerViewControllerAnimated:tmpMoviePlayViewController];
tmpMoviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieViewFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:tmpMoviePlayViewController];
[tmpMoviePlayViewController.moviePlayer play];
}
}else{
MPMoviePlayerController* theMovie = [[[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]] retain];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
[theMovie play];
}
}
The movie plays fine and disappears when 'Done' is pressed but the callbacks are never called. Any suggestions?
电影播放正常并在按下“完成”时消失,但从未调用回调。有什么建议?
Thanks.
谢谢。
回答by cweinberger
Same problem here. Figured out, when I send nil
as object (instead of the MoviePlayerController
) the callback is triggered...
同样的问题在这里。想通了,当我nil
作为对象(而不是MoviePlayerController
)发送时,会触发回调......
回答by Andrew Koransky
I had the same problem. This postsaved me. If the video goes full screen, capture MPMoviePlayerDidExitFullscreenNotification
instead of MPMoviePlayerPlaybackDidFinishNotification
. I capture both below just in case I change my mind later.
我有同样的问题。 这个帖子救了我。如果视频全屏显示,则捕获MPMoviePlayerDidExitFullscreenNotification
而不是MPMoviePlayerPlaybackDidFinishNotification
。我将两者都记录在下面,以防万一我以后改变主意。
- (void)videoButtonClick:(id)sender {
MPMoviePlayerController* moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:theVideoURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayerController];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[self.view addSubview:moviePlayerController.view];
moviePlayerController.shouldAutoplay = YES;
moviePlayerController.initialPlaybackTime = 0;
moviePlayerController.scalingMode = MPMovieScalingModeAspectFit;
moviePlayerController.fullscreen = YES;
[moviePlayerController play];
}
- (void)moviePlayBackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController* moviePlayerController = notification.object;
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayerController];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayerController];
moviePlayerController.initialPlaybackTime = -1;
[moviePlayerController stop];
[moviePlayerController release];
}
回答by Hardik Mamtora
Make sure for
确保为
moviePlayer.repeatMode = MPMovieRepeatModeNone;
回答by Bruno Tereso
i realize this is a old question, but none of the above solved my problems. If you end up trying everything like i did, make sure your parent view controller aint using the same method for notification. I was sending the notification to the wrong place.
我意识到这是一个老问题,但以上都没有解决我的问题。如果您最终像我一样尝试了所有方法,请确保您的父视图控制器没有使用相同的通知方法。我将通知发送到错误的地方。
Change the selector name for something less usual and try again.
更改不太常用的选择器名称,然后重试。
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MyMovieClosedThisTimeForReal:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
回答by Selvin
You can observe to UIWindowDidBecomeVisibleNotification
and UIWindowDidBecomeHiddenNotification
as stated in this post. This works even in iOS 8.
你可以观察到UIWindowDidBecomeVisibleNotification
,并UIWindowDidBecomeHiddenNotification
在规定这个岗位。这甚至适用于 iOS 8。
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowVisible:)
name:UIWindowDidBecomeVisibleNotification
object:self.view.window];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(windowHidden:)
name:UIWindowDidBecomeHiddenNotification
object:self.view.window];
- (void)windowVisible:(NSNotification *)notification
{
NSLog(@"-windowVisible");
}
- (void)windowHidden:(NSNotification *)notification
{
NSLog(@"-windowHidden");
}
回答by Shaked Sayag
I also had this problem and no other solution worked for me. The problem is that when adding the observer, the object parameter should be tmpMoviePlayViewController.moviePlayer and not just moviePlayer.
我也遇到了这个问题,没有其他解决方案对我有用。问题是在添加观察者时,对象参数应该是 tmpMoviePlayViewController.moviePlayer 而不仅仅是moviePlayer。
The tmpMoviePlayViewController.moviePlayer (class MPMoviePlayerController) is the one sending the notification, not the tmpMoviePlayViewController (class MPMoviePlayerViewController).
tmpMoviePlayViewController.moviePlayer(类 MPMoviePlayerController)是发送通知的,而不是 tmpMoviePlayViewController(类 MPMoviePlayerViewController)。
So change this:
所以改变这个:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myMovieViewFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification object:tmpMoviePlayViewController];
to this:
对此:
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(myMovieViewFinishedCallback:)
name:MPMoviePlayerPlaybackDidFinishNotification object:tmpMoviePlayViewController.moviePlayer];