I have an universal application that plays movies from the internet. It has to support 3.1.x as well as 4.x.
In order to get this to work, I have a branch in the code that detects pre-3.2 devices and utilizes MPMoviePlayerController as it is supposed to work there.
This is how I prepare the player to play the remote movie:
- (void)registerForMovieNotifications {
//for 3.2 devices and above
if ([moviePlayer respondsToSelector:@selector(loadState)]) {
LOG(@"moviePlayer responds to loadState, this is a 3.2+ device");
//register the notification that the movie is ready to play
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayerLoadStateChanged:)
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(didExitFullScreen:)
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
LOG(@"preparing moviePlayer...");
[moviePlayer prepareToPlay];
} else {
//for pre-3.2 devices
LOG(@"This is a 3.1.x device");
//register the notification that the movie is ready to play
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePreloadDidFinish:)
name:MPMoviePlayerContentPreloadDidFinishNotification
object:nil];
}
//handle when the movie finished
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
}
- (void)readyPlayer {
if (!moviePlayer) {
moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
} else {
[moviePlayer setContentURL:movieURL];
}
[self registerForMovieNotifications];
}
Later on I get this notification, and it sets up the movie player's view, etc.
- (void) moviePlayerLoadStateChanged:(NSNotification*)notification {
LOG(@"3.2/4.x - moviePlayerLoadStateChanged:");
//unless state is unknown, start playback
if ([moviePlayer loadState] != MPMovieLoadStateUnknown) {
//remove observer
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerLoadStateDidChangeNotification
object:nil];
//set the frame of the movie player to match
self.view.autoresizesSubviews = YES;
[[moviePlayer view] setFrame:self.view.bounds];
[[moviePlayer view] setAutoresizingMask:UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight];
[[moviePlayer view] setAutoresizesSubviews:YES];
//add movie player as a subview
[self.view addSubview:moviePlayer.view];
[moviePlayer setFullscreen:YES];
//play the movie
[moviePlayer play];
}
}
And the movie plays. This works perfectly on iPhone 4.2, 4.3, iPad 4.2, 4.3, but it fails on iPad 3.2. The movie plays but all I get is a black screen.
If I remove the [moviePlayer setFullscreen:YES] call I get a visible playing movie in 3.2, however it isn't "fullscreen" and so it doesn't have the Done button and there's no way for me to dismiss the screen.
I'd love some help on what's going on here. Thanks!
setFullscreen:animated:instead, just in case? - Nathan Eror