如何在MPMoviePlayerController上播放失败时获取错误说明 [英] How to get an error description when playback fails on MPMoviePlayerController

查看:128
本文介绍了如何在MPMoviePlayerController上播放失败时获取错误说明的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果视频播放失败,我想显示 UIAlert 。所以我为我的电影播放器​​注册了 MPMoviePlayerPlaybackDidFinishNotification

I want to show an UIAlert if the Video-Play fails. So i registered the MPMoviePlayerPlaybackDidFinishNotification for my Movie Player:


[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback :) name:MPMoviePlayerPlaybackDidFinishNotification object:self.movi​​ePlayer];

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];

在myMovieFinishedCallback中:我检查如果在用户信息字典中是一个名为错误的对象。在我的真实设备上,我没有收到此错误(没有网络错误,文件404错误)。在iPhone模拟器上我收到错误。

In my myMovieFinishedCallback: I check if in the User Info Dictionary is a Object named error. On my real device I don't get this error (on no network error, 404 error for file). On the iPhone Simulator I receive the error.

如何在收到 MPMoviePlayerPlaybackDidFinishNotification 时正确检查推理?

How can I properly check the reasoning when I receive the MPMoviePlayerPlaybackDidFinishNotification?

推荐答案

不幸的是, MPMoviePlayerController (直到但不包括iOS 4.3)没有从文档中提供的问题详细说明问题。如果在 MPMoviePlayerPlaybackDidFinishNotification 的UserInfo中出现任何问题,它只返回 MPMovieFinishReasonPlaybackError

Unfortunately, MPMoviePlayerController (up until but not including iOS 4.3) has no verbose identification of problems from what is available from the documentation. It simply returns MPMovieFinishReasonPlaybackError in case of any problem within the UserInfo of that MPMoviePlayerPlaybackDidFinishNotification.

在iOS 4.3中,我们终于得到了 errorLog accessLog 属性包含扩展和非常有用的信息。
请参阅 MPMoviePlayerController参考

With iOS 4.3 we finally got the errorLog and accessLog properties containing extended and pretty helpful information. See MPMoviePlayerController Reference.

对于iOS 5.0,还有一个错误密钥,该通知也会出现在设备构建中,而不仅仅是在模拟器中。 错误 NSError 的一个实例,并提供非常有用的信息。不幸的是,Apple没有记录,因此它可能会在iOS的任何版本中发生变化。此外,似乎没有解释给定的错误代码。例如,HTTP-Status:404将导致给定错误实例中的错误代码 -1100 。但是,这将是如何以最恰当的方式处理此通知的示例。

With iOS 5.0 there is an error key coming with that notification also on device builds and not just within the simulator. That error is an instance of NSError and supplies very helpful information. Unfortunately that has not been documented by Apple, hence it may change at any release of iOS. Additionally, there seems to be no explanation on the given error-codes. For example an HTTP-Status:404 would result into an error-code -1100 within the given error instance. However, this would be an example of how to handle this notification in the most proper way.

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

这将是一个合适的通知处理程序:

That would be a proper notification handler:

- (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");
        }
    }
}

最后但并非最不重要的是,做在发布您正在处理它的对象的实例时,不要忘记从默认中心删除该通知处理程序。

Last but not least, do not forget to remove that notification handler from the default center when releasing the instance of the object you are handling it within.

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

这篇关于如何在MPMoviePlayerController上播放失败时获取错误说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆