为什么MPMoviePlayerViewController具有相同URL的不同行为?

时间:2015-07-29 07:27:18

标签: objective-c ios8 streaming mpmovieplayercontroller

我在我的应用程序中实现了MPMovieViewController但它有一个问题,即当我在播放器中播放视频时,第一次播放失败但下次它将使用相同的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];

1 个答案:

答案 0 :(得分:0)

我建议您处理错误,看看您的网址视频会发生什么(这是我很久以前创建的一条提示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];

通过这种解决方法,你应该知道你的流的错误,我希望这有帮助!

相关问题