Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 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 !

like image 476
Francescu Avatar asked Nov 14 '22 11:11

Francescu


1 Answers

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.

like image 52
Christian Avatar answered Dec 19 '22 11:12

Christian