【问题标题】:why MPMoviePlayerViewController having different behavior with the same URL?为什么 MPMoviePlayerViewController 对相同的 URL 有不同的行为?
【发布时间】:2015-07-29 07:27:17
【问题描述】:

我在我的应用程序中实现了 MPMovieViewController,但它有一个问题,即当我在播放器中播放视频时,第一次播放失败,但下一次它将使用相同的 URL 成功播放。我无法使用相同的 URL 理解这种不同的行为。我在这里复制了我在我的应用程序中使用的代码。

NSURL *fileURL = [NSURL URLWithString:@"http://nordenmovil.com/urrea/InstalaciondelavaboURREAbaja.mp4"];
MPMoviePlayerViewController * controller = [[MPMoviePlayerViewController alloc]initWithContentURL:fileURL];
controller.moviePlayer.movieSourceType= MPMovieSourceTypeFile;
[controller.moviePlayer setControlStyle:MPMovieControlStyleDefault];
[controller.moviePlayer prepareToPlay];
[controller.moviePlayer play];
[self presentMoviePlayerViewControllerAnimated:controller];

【问题讨论】:

    标签: objective-c ios8 streaming mpmovieplayercontroller


    【解决方案1】:

    我建议处理错误以查看您的 URL 视频会发生什么(这是我很久以前创建的提示 in this answer

    添加此以捕获播放结束:

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

    看看会发生什么:

    - (void)handleMPMoviePlayerPlaybackDidFinish:(NSNotification *)notification
    {
        NSDictionary *notificationUserInfo = [notification userInfo];
        NSNumber *resultValue = [notificationUserInfo objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
        MPMovieFinishReason reason = [resultValue intValue];
        if (reason == MPMovieFinishReasonPlaybackError)
        {
            NSError *mediaPlayerError = [notificationUserInfo objectForKey:@"error"];
            if (mediaPlayerError) 
            {
                NSLog(@"playback failed with error description: %@", [mediaPlayerError localizedDescription]);
            }
            else
            {
                NSLog(@"playback failed without any given reason");
            }
        }
    }
    

    不要忘记删除通知处理程序:

    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                    name:MPMoviePlayerPlaybackDidFinishNotification
                                                  object:nil];
    

    通过此解决方法,您应该知道流中的错误,我希望这会有所帮助!

    【讨论】:

    • 谢谢,当我从 URL 播放视频时出现错误 error = "Error Domain=MediaPlayerErrorDomain Code=-11800 \"The operation could not be completed\" 并且 error = "Error Domain=MediaPlayerErrorDomain Code=-11819 播放本地存储中的视频时“无法完成操作”
    • 你是在模拟器还是真机上试用?
    • 我正在尝试在 iPhone 和 iPad 上运行 iOS 8.1、iOS 8.2、iOS 8.3 和 8.4
    • 有什么解决办法吗?
    猜你喜欢
    • 1970-01-01
    • 2014-05-20
    • 2018-01-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-22
    相关资源
    最近更新 更多