按下主页按钮时,MPMoviePlayerViewController消失

时间:2012-09-12 03:22:51

标签: iphone objective-c ios xcode mpmovieplayercontroller

我正在创建我的MPMoviePlayerViewController:

MPMoviePlayerViewController *playerView = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[self presentMoviePlayerViewControllerAnimated:playerView];

直到我按下主页按钮才能正常工作:这样,在我重新打开应用程序后,播放器视图控制器消失了,我看到了我最初调用它的视图控制器。如果我双击主页按钮,打开多任务面板,播放器不会消失,只是暂停(应该如此)。

那么如何防止这种情况发生(关闭并重新打开应用程序会让我重新播​​放视频)?

UPD: 我注意到,如果我在“正在加载...”文本启动时关闭并重新打开应用程序,则播放器不会消失。在这种情况下,回到应用程序让玩家继续加载视频,最终开始播放。然而,在此之后关闭应用程序(视频播放或暂停时)不可避免地会杀死播放器视图控制器......

4 个答案:

答案 0 :(得分:1)

试试这样:

 MPMoviePlayerViewController *playerView = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
    playerView.view.frame = self.view.frame;
    [self presentMoviePlayerViewControllerAnimated:playerView];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(MPMoviePlayerDidExitFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
    [[NSNotificationCenter defaultCenter]
     addObserver:self
     selector:@selector(playbackStateChanged:)
     name:MPMoviePlayerPlaybackStateDidChangeNotification
     object:moviePlayerController];

     [playerView.moviePlayer play];

之后添加这两种方法:

- (void) movieFinishedCallback:(NSNotification*) aNotification 
{
    moviePlayerController = [aNotification object];
    [moviePlayerController.moviePlayer stop];
    [[NSNotificationCenter defaultCenter] 
     removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification
     object:moviePlayerController];
    [moviePlayerController autorelease];
    NSLog(@"stopped?");
}

- (void)MPMoviePlayerDidExitFullscreen:(NSNotification *)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerDidExitFullscreenNotification 
                                                  object:nil];

    [moviePlayerController.moviePlayer stop];
    MPMoviePlayerController *player = [notification object];
    [[NSNotificationCenter defaultCenter] 
     removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification
     object:player];
    [moviePlayerController dismissMoviePlayerViewControllerAnimated];
}

答案 1 :(得分:1)

我在这里找到了答案:

MPMoviePlayerViewController gets dismissed, when app resignes active state

Required values in plist

该问题的作者(和答案)提到他的应用程序因此被拒绝。

答案 2 :(得分:0)

你试试这个代码---

 - (void)willEnterFullscreen:(NSNotification*)notification {
NSLog(@"willEnterFullscreen");
}

- (void)enteredFullscreen:(NSNotification*)notification {
NSLog(@"enteredFullscreen");
}

- (void)willExitFullscreen:(NSNotification*)notification {
NSLog(@"willExitFullscreen");
}

- (void)exitedFullscreen:(NSNotification*)notification {
NSLog(@"exitedFullscreen");
[self.movieController.view removeFromSuperview];
self.movieController = nil;
[[NSNotificationCenter defaultCenter] removeObserver:self];
}

- (void)playbackFinished:(NSNotification*)notification {
NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
switch ([reason intValue]) {
    case MPMovieFinishReasonPlaybackEnded:
        NSLog(@"playbackFinished. Reason: Playback Ended");         
            break;
    case MPMovieFinishReasonPlaybackError:
        NSLog(@"playbackFinished. Reason: Playback Error");
            break;
    case MPMovieFinishReasonUserExited:
        NSLog(@"playbackFinished. Reason: User Exited");
            break;
    default:
        break;
}
[self.movieController setFullscreen:NO animated:YES];
}

- (void)showMovie {
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willEnterFullscreen:) name:MPMoviePlayerWillEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(willExitFullscreen:) name:MPMoviePlayerWillExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(enteredFullscreen:) name:MPMoviePlayerDidEnterFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(exitedFullscreen:) name:MPMoviePlayerDidExitFullscreenNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];

NSURL* movieURL =  [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"tron" ofType:@"mov"]];
self.movieController = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
self.movieController.view.frame = self.view.frame;
[self.view addSubview:movieController.view];
[self.movieController setFullscreen:YES animated:YES];
[self.movieController play];
}

答案 3 :(得分:0)

当应用程序进入后台时,MPMoviePlayerViewControllers会关闭,如Arnold所述,当您离开嵌入的视图控制器视图时,也可以忽略。要解决这两个问题,请尝试将MPMoviePlayerViewController保留为ivar或属性并重置当vc的viewWillAppear:被调用或者应用程序重新进入前台时,就像:

- (void) viewDidLoad{
      [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reset) name:UIApplicationWillEnterForegroundNotification object:nil];
}    

- (void) viewWillAppear:(BOOL)animated{
      [self reset];
}

- (void) reset{
      [_mpmpvc.moviePlayer prepareToPlay];
      [_mpmpvc.moviePlayer pause];
}