2

我正在创建我的 MPMoviePlayerViewController,如下所示:

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

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

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

UPD:我注意到如果我在“正在加载...”文本出现时关闭并重新打开应用程序,播放器不会消失。在这种情况下,回到应用程序可以让播放器继续加载视频以最终开始播放。然而,在那之后关闭应用程序(无论是在视频播放还是暂停时)不可避免地会杀死播放器视图控制器......</p>

4

4 回答 4

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];
}
于 2012-09-12T03:49:40.043 回答
1

我在这里找到了答案:

当应用程序退出活动状态时,MPMoviePlayerViewController 被解雇

plist 中的必需值

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

于 2012-09-12T04:40:31.783 回答
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];
}
于 2012-09-12T05:43:54.447 回答
0

正如 Arnold 所说,当应用程序进入后台时 MPMoviePlayerViewControllers 会关闭,当您离开它所嵌入的视图控制器视图时也可以关闭。要解决这两个问题,请尝试将 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];
}
于 2014-01-04T05:16:55.457 回答