2

Hey, I use MPMoviePlayerViewController to display video. I don't know how to handle network problems. I would like to dismiss the MPMoviePlayerViewController controller on error. The dismissMoviePlayerViewControllerAnimated method works only the first time, the second time I get black screen.

Example code:

// VideoViewController.h
#import <MediaPlayer/MediaPlayer.h>
@interface VideoViewController : MPMoviePlayerViewController 
{
}
@end

// VideoViewController.m
@implementation VideoViewController
- (void)viewDidLoad
{
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieDidLoad:) name:MPMoviePlayerContentPreloadDidFinishNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(movieDidLoad:) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
}

-(void)movieDidLoad:(NSNotification*)notification
{
    [self dismissMoviePlayerViewControllerAnimated];
}
@end

// XController's function to call it
- (void)showVideoView
{
    VideoViewController * controller = [[VideoViewController alloc] initWithContentURL:[NSURL URLWithString:@"http://myvideos.com/movie.m4v"]];
    [self presentMoviePlayerViewControllerAnimated:controller];
    [controller.moviePlayer play];
    [controller release];
}

Please tell me how to handle network problems. Note also, that video is always in fullscreen.

xpepermint
  • 35,055
  • 30
  • 109
  • 163

1 Answers1

2

Why you have created VideoViewController any special reason? You can do that all things without creating that if you want to customize something than it's okay. Other thing is that for both notification you have registered "movieDidLoad " this method and this will dismiss your view.When video will be ready to play then you'r view will be dismiss due to this method you have registered for the " MPMoviePlayerContentPreloadDidFinishNotification ". This link will help you more:

- (void)playbackFinished:(NSNotification*)notification {
NSNumber* reason = [[notification userInfo] objectForKey:MPMoviePlayerPlaybackDidFinishReasonUserInfoKey];
switch ([reason intValue]) {
    case MPMovieFinishReasonPlaybackEnded:
        NSLog(@"playbackFinished. Reason: Playback Ended");         
        break;
    case MPMovieFinishReasonPlaybackError:
        NSLog(@"playbackFinished. Reason: Playback Error");
        break;
        case MPMovieFinishReasonUserExited:
        NSLog(@"playbackFinished. Reason: User Exited");
        break;
    default:
        break;
}
[self.movieController setFullscreen:NO animated:YES];

}

Iducool
  • 3,543
  • 2
  • 24
  • 45
  • yes... to I also override shouldAutorotateToInterfaceOrientation method. – xpepermint Apr 02 '11 at 13:16
  • There is one property of notification class "userinfo". It will return you Nsdictionary. This dictionary contains two important thing one is error code and other is playfinish reason.from error code you can get exact reason for failing. And you can make you'r application more error prone. I don't have pc right now so can't say exactly you have to debug. – Iducool Apr 02 '11 at 13:41
  • if I don't execute [[notification object] stop];, [self dismissMoviePlayerViewControllerAnimated] is not working but calling stop it means that MPMovieFinishReasonPlaybackError is called twice. – xpepermint Apr 02 '11 at 14:25