views:

472

answers:

3

Hi,

Since 3.2 iPhone OS SDK, playing a video is really different.

So I was wondering 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 !

A: 

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.

JBRWilkinson
Using preprocessor macros like that will have the effect of doing whatever the Active SDK is set to. When you compile the app, if you're targeting, say, both 3.1.3 and a later version of the iPhone SDK, it will compile the app with the 3.1.3 settings and the same binary will be used for both OS versions. In general, run-time introspection (e.g. `if ([SomeClass respondsToSelector:@selector(someSelector)])`) is preferred.
Jeff Kelley
A: 

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
    }
}
David Liu
+1  A: 

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.

Christian