MPMoviePlayerViewController 没有在 2nd 播放 运行

MPMoviePlayerViewController not playing on 2nd run

我创建了一个播放实时视频的 MPMoviePlayerViewController。但是,如果我播放视频两次意味着打开播放器,单击完成,然后再次播放流。结果只是黑屏,没有 MPMoviePlayerViewController 的控件。而且我需要停止模拟器,因为我认为应用程序正在崩溃。这是我的做法

- (void) playUrl:(NSURL *)movieInfo
{
    NSURL *streamUrl   = movieInfo;
    MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:streamUrl];
    [[mpvc view] setFrame:self.view.bounds];
    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(movieFinishedCallback:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:nil];

    mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
    [mpvc.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
    [mpvc.moviePlayer setShouldAutoplay:YES];
    [mpvc.moviePlayer setFullscreen:NO animated:YES];
    [mpvc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    [mpvc.moviePlayer setScalingMode:MPMovieScalingModeNone];
    [mpvc.moviePlayer setUseApplicationAudioSession:NO];
    [self presentMoviePlayerViewControllerAnimated:mpvc];

}


- (void) movieFinishedCallback:(NSNotification*) aNotification 
{
    MPMoviePlayerController *player = [aNotification object];

    [player stop];

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:player];
    [player.view removeFromSuperview];

    NSLog(@"stopped?");
}

我看到在您的 movieFinishedCallback: 实施中,您删除了 MPMoviePlayerController 视图,但在您的 playUrl: 实施中,您只是设置了视图的框架,大概是在您已经在 viewDidLoad.

中添加了视图

一个值得尝试的明显变化是更新您的代码以使用 AVKit 框架中的 AVPictureInPictureControllerAVPlayerViewController class,或 WKWebView class 来自 WebKit。根据 MPMoviePlayerViewController docs,自 iOS 9:

起已弃用

The MPMoviePlayerViewController class is formally deprecated in iOS 9. (The MPMoviePlayerController class is also formally deprecated.) To play video content in iOS 9 and later, instead use the AVPictureInPictureController or AVPlayerViewController class from the AVKit framework, or the WKWebView class from WebKit.

尝试将视图添加到层​​次结构的行移动到 playUrl: 方法。通常,为您的事件对应方采用相反方法的反击实现是一种很好的做法。例如,实现一个方法来在事件开始时构建和添加视图,并有一个相应的方法在同一事件结束时拆除和删除相同的视图。但是,我说 'generally' 是因为总是有例外,而且您可能有非常令人信服的理由不这样做。因此,在这种情况下,相反的调用是 presentMoviePlayerViewControllerAnimated:dismissMoviePlayerViewControllerAnimated:,可从 UIViewController 类别中获得。

view 访问权限更改为使用点符号后,为了与您的回调实现保持一致,假设您要添加视图,这就是您的新 playUrl: 实现的样子到 self.view:

- (void) playUrl:(NSURL *)movieInfo
{
    NSURL *streamUrl   = movieInfo;
    MPMoviePlayerViewController *mpvc = [[MPMoviePlayerViewController alloc] initWithContentURL:streamUrl];
    [mpvc.view setFrame:self.view.bounds];
    [self.view addSubview:mpvc.view];

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieFinishedCallback:)
                                                 name:MPMoviePlayerPlaybackDidFinishNotification
                                               object:nil];

    mpvc.moviePlayer.movieSourceType = MPMovieSourceTypeStreaming;
    [mpvc.moviePlayer setControlStyle:MPMovieControlStyleFullscreen];
    [mpvc.moviePlayer setShouldAutoplay:YES];
    [mpvc.moviePlayer setFullscreen:NO animated:YES];
    [mpvc setModalTransitionStyle:UIModalTransitionStyleCoverVertical];
    [mpvc.moviePlayer setScalingMode:MPMovieScalingModeNone];
    [mpvc.moviePlayer setUseApplicationAudioSession:NO];
    [self presentMoviePlayerViewControllerAnimated:mpvc];

}

另一种选择是不在回调方法中删除玩家的视图。如果那不是罪魁祸首,那么我接下来要调查的是检查您是否正在向 nil 对象发送消息。另外,看看当你从 movieFinishedCallback: 中取出所有实现时会发生什么,除了获取和停止播放器。

希望对您有所帮助!

通过删除 [player.view removeFromSuperview] 行解决了问题