【问题标题】:MPMoviePlayerViewController not playing on 2nd runMPMoviePlayerViewController 在第二次运行时不播放
【发布时间】:2016-07-14 03:31:26
【问题描述】:

我创建了一个播放实时视频的 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?");
}

【问题讨论】:

    标签: objective-c mpmovieplayercontroller


    【解决方案1】:

    我看到在您的movieFinishedCallback: 实现中,您删除了MPMoviePlayerController 视图,但在您的playUrl: 实现中,您只是设置了视图的框架,大概是在您已经在viewDidLoad 中添加了视图之后。

    一个值得尝试的明显更改是更新您的代码以使用来自AVKit 框架的AVPictureInPictureControllerAVPlayerViewController 类,或来自WebKitWKWebView 类。根据MPMoviePlayerViewControllerdocs,iOS 9 已弃用:

    MPMoviePlayerViewController 类在 iOS 9 中已正式弃用。(MPMoviePlayerController 类也已正式弃用。)要在 iOS 9 及更高版本中播放视频内容,请改用 AVKit 框架中的 AVPictureInPictureController 或 AVPlayerViewController 类,或从网络套件。

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

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

    - (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: 中取出所有实现时会发生什么,除了获取和停止播放器。

    希望对你有帮助!

    【讨论】:

    • 尝试关闭其视图当前未出现的模态视图控制器。 self = modalViewController = 我收到这条消息
    • 嗨,我还看到你有一个电话给presentMoviePlayerViewControllerAnimated。是否使用 UIViewController's presentMoviePlayerViewControllerAnimated 方法呈现?如果是这样,您应该使用 dismissXXX 对应项将其删除。
    • 我还更新了我的答案,建议更新为AVPictureInPictureControllerAVPlayerViewController,,因为MPMoviePlayerViewController 已被弃用。
    【解决方案2】:

    通过删除 [player.view removeFromSuperview] 行修复了问题

    【讨论】:

    • 恭喜!我很高兴它奏效了。请检查我的答案!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 2021-06-18
    • 2011-01-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多