iPhone - Play a video on both 3.0 and 4.0 OS / SDK?
Since 3.2 iPhone OS SDK, playing a video is really different.
So I was won开发者_开发知识库dering if there is a way to make video play in full screen with a compatible code (both < and >3.2) without writing code for the two cases.
I think we'll have to write 2 versions of our classes handling video playing...
Thy !
I do basically what Jeff Kelly above suggests to run on 3.1 and above, note the instancesRespondToSelector call:
// Initialize a movie player object with the specified URL
MPMoviePlayerController *mp = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
if (mp)
{
// Register to receive a notification when the movie has finished playing.
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:nil];
//Will only run this code for >= OS 3.2
if ([MPMoviePlayerController instancesRespondToSelector:@selector(setFullscreen:animated:)]){
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackStateDidChange:)
name:MPMoviePlayerPlaybackStateDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(nowPlayingMovieDidChange:)
name:MPMoviePlayerNowPlayingMovieDidChangeNotification
object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlayBackDidFinish:)
name:MPMoviePlayerDidExitFullscreenNotification
object:nil];
mp.controlStyle = MPMovieControlStyleFullscreen;
[mp setScalingMode:MPMovieScalingModeAspectFit];
//change mainMenu here to whatever your parent view is
[mp.view setFrame:mainMenu.frame];
[self.view addSubview:mp.view];
[mp setFullscreen:YES animated:NO];
}
//continue as normal
and then later in the moviePlayBackDidFinish function I use the same technique to remove the notifications.
One possibility is to have a helper method for this. This way you'll only need to write once and have this capability everywhere.
To write the helper method itself, you'll want to check if MPMoviePlayerViewController is available. If so, use that, and then present that fullscreen. Otherwise just use the regular MPMoviePlayerController.
So the basic framework would be:
-(void)playMovie:(NSURL *)movieURL
{
Class mpVC = NCClassFromString("MPMoviePlayerViewController");
if(mpVC)
{
// Generate MPPlayerViewController here and use accordingly
}
else
{
// Generate MPPlayerController here and use accordingly
}
}
You may have to use #if/#else/#endif blocks and compile a Universal Binary which has the right executable for the particular O/S level.
精彩评论