Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
225 views
in Technique[技术] by (71.8m points)

ios - How to get an error description when playback fails on MPMoviePlayerController

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.moviePlayer];

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.

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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

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];

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...