I was using MPMoviePlayer to play a short video in my app with no problems in SDK 3.1.3. I made the changes to the code in SDK 4 but the video is not playing. I just get a black screen and audio. The Apple Dev Center doesnt have any sample code for this class for the latest SDK. Following is the code I'm using:
(void)viewDidLoad {
[super viewDidLoad];
//videoPlayer is a MPMoviePlayerController object defined in the header file of the view controller
if (videoPlayer == nil){ NSString * videoPath = [[NSBundle mainBundle] pathForResource:@"myvideo" ofType:@"mp4"]; if (videoPath == NULL){ return; } NSURL * videoURL = [NSURL fileURLWithPath:videoPath];
videoPlayer = [[MPMoviePlayerController alloc] initWithContentURL:videoURL]; videoPlayer.controlStyle = MPMovieControlStyleNone; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:@"MPMoviePlayerPlaybackDidFinishNotification" object:movieController.moviePlayer]; [videoPlayer play]; [videoPlayer setFullscreen:YES]; [self.view addSubview:videoPlayer.view];
}
}
The above results in just the audio being played with a black screen. The notification is called correctly at the end of the playback.
When the above did not work, then I even tried using the new MPMoviePlayerViewController class as follows:
(void)viewDidLoad {
[super viewDidLoad]; NSString * videoPath = [[NSBundle mainBundle] pathForResource:@"myvideo" ofType:@"mp4"];
if (videoPath == NULL){ return; } NSURL * videoURL = [NSURL fileURLWithPath:videoPath];
//movieController is an MPMoviePlayerViewController object defined in the header file of view controller
movieController = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playbackFinished:) name:@"MPMoviePlayerPlaybackDidFinishNotification" object:movieController.moviePlayer];
[movieController.moviePlayer setFullscreen:YES]; [movieController.moviePlayer play]; [self presentMoviePlayerViewControllerAnimated:movieController]; }
Same problem persists - I can hear the audio and the notification at the end of the playback is called as expected. However I just see a black screen instead of the video.
There is nothing wrong in the encoding of the video because the same video plays fine in iTunes as well as on my iPod Touch in the regular videos playlist.
Could anyone help me out with this problem?
Thanks in advance