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
?
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];
I had the same problem. althought i was registering the MPMoviePlayerLoadStateDidChangeNotification callback function and getting the error from the userInfo variable it was not showing the error. I spent a lot of time searching through forums and after playing around with the code i realized what the problem was.
First you need to register the callback function:
// Register that the did finish notification (movie stopped)
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:self.moviePlayer];
Within the MovieFinished callback function you will need:
NSDictionary *notice = [paramNotification userInfo];
if (notice != nil)
{
NSError *errorInfo = [notice objectForKey:@"error"];
if ( errorInfo != nil ) {
UIAlertView *notice = [[UIAlertView alloc] initWithTitle:@"Error" message:[errorInfo localizedDescription] delegate:self cancelButtonTitle:@"OK" otherButtonTitles:nil];
[notice show];
[notice release];
}
}
This code will show any error related to the moviecontroller. So.. what was the problem in my code?.. I was using the [moviecontroller play] method in the wrong places, so check that in yours.
Good luck!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With