ios - 视频播放后无法返回 View

标签 ios

我正在运行以下代码。视频播放得很好,但播放完毕后,它只是变成黑屏,我原来的 View 再也没有回来。当我点击黑屏时,我只看到消息“正在加载......”有人可以解释一下我做错了什么吗?谢谢

- (IBAction)video:(UIBarButtonItem *)sender
{
    {
        NSURL *url = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                             pathForResource:@"IMG_0973" ofType:@"MOV"]];
        moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:url];


        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDidFinish:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePlayBackDonePressed:) name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];



        moviePlayer.controlStyle=MPMovieControlStyleDefault;
        //moviePlayer.shouldAutoplay=NO;
        [moviePlayer play];
        [self.view addSubview:moviePlayer.view];
        [moviePlayer setFullscreen:YES animated:YES];

    }

}

- (void) moviePlayBackDonePressed:(NSNotification*)notification
{
    [moviePlayer stop];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerDidExitFullscreenNotification object:moviePlayer];


    if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)])
    {
        [moviePlayer.view removeFromSuperview];
    }

    moviePlayer=nil;
}

- (void) moviePlayBackDidFinish:(NSNotification*)notification
{
    [moviePlayer stop];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer];

    if ([moviePlayer respondsToSelector:@selector(setFullscreen:animated:)])
    {
        [moviePlayer.view removeFromSuperview];
    }
}

最佳答案

添加此通知方法

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(moviePreloadDidFinish:) name:MPMoviePlayerLoadStateDidChangeNotification
                                               object:player];

此方法在您的电影加载后调用,并在该方法中添加您的 moviePlayer View 。

-(void)moviePreloadDidFinish:(NSNotification*)notification
{

   moviePlayer.controlStyle=MPMovieControlStyleDefault;
   [self.view addSubview:moviePlayer.view];
   [moviePlayer play];
   [moviePlayer setFullscreen:YES animated:YES];

}

关于ios - 视频播放后无法返回 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16512640/

相关文章:

ios - alljoyn框架ios错误: cannot parse the debug map for

ios - 如何在不登录 Xcode 8 的情况下构建 IPA

javascript - 如何在 React Native 中的当前屏幕上显示 FlatList 时主动刷新它?

ios - 具有全局变量的共享实例

ios - 如何使 Swift 中的 UILabel 成为一个圆圈

ios - 执行segue不工作

ios - UIViewAnimationOptionBeginFromCurrentState 基本动画的意外行为

ios - 适用于 iOS 的 Firebase 集成示例 (FriendlyPix)

ios - 在 iOS : what is the difference between capturing video using AVMovieFileOutput or AVAssetWriter? 上捕获视频

ios - 我应该如何在IOS应用程序中存储我的静态文本数据