MPMoviePlayerController for iPhone under iOS4, iOS4, and iPad

Discussion in 'iPhone/iPad Programming' started by newtoiphonesdk, Aug 17, 2010.

  1. macrumors 6502a

    Joined:
    Jul 30, 2010
    #1
    I have an app that I originally made before iOS4 was released. It has a video embedded in it, and to have it play in the iPad as well as the iPhone I had this code
    Code:
    - (IBAction)play {
    #if __IPHONE_OS_VERSION_MAX_ALLOWED >= 30200
    	
    	if( UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPad )
    	{
    		NSBundle *bundle = [NSBundle mainBundle];
    		NSString *moviePath = [bundle pathForResource:@"welcome" ofType:@"mp4"];
    		NSURL  *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
    		MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    		theMovie.scalingMode = MPMovieScalingModeAspectFill;
    		[theMovie play];
    		MPMoviePlayerViewController *moviePlayer = [[MPMoviePlayerViewController alloc] initWithContentURL:movieURL];
    		[self presentMoviePlayerViewControllerAnimated:moviePlayer];
    	}
    	else
    	{
    		NSBundle *bundle = [NSBundle mainBundle];
    		NSString *moviePath = [bundle pathForResource:@"welcome" ofType:@"mp4"];
    		NSURL  *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
    		MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    		theMovie.scalingMode = MPMovieScalingModeAspectFill;
    		[theMovie play];
    	}
    	
    #else
    	
    	// iPhone simulator
    	NSBundle *bundle = [NSBundle mainBundle];
    	NSString *moviePath = [bundle pathForResource:@"welcome" ofType:@"mp4"];
    	NSURL  *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
    	MPMoviePlayerController *theMovie = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
    	theMovie.scalingMode = MPMovieScalingModeAspectFill;
    	[theMovie play];
    	
    #endif
    }
    
    Now, I need the same code for iOS4, as I did the iPad, however it isn't working. How can I change this to play in the iPad, iOS4, and older iPhones still?
     
  2. macrumors newbie

    sherry2009

    Joined:
    Dec 14, 2009
    #2
    Code:
    -(void)myMovieFinishedCallback:(NSNotification*)aNotification
     {      
    MPMoviePlayerController* theMovie=[aNotification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
    [theMovie release]; [[UIApplication sharedApplication]
    setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
    }
    -(void)myMovieViewFinishedCallback:(NSNotification*)aNotification {
     MPMoviePlayerViewController* theMovieView=[aNotification object];
    [self dismissMoviePlayerViewControllerAnimated];
     [[NSNotificationCenter defaultCenter]removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovieView];
    [theMovieView release];
    [[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
    }
    - (void) startPlayback : (id) sender {
    NSString *path = [[NSBundle mainBundle] pathForResource:@"dream" ofType:@"mp4"];
    if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2)
    {
    MPMoviePlayerViewController*tmpMoviePlayViewController=[[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]] retain];
    if (tmpMoviePlayViewController) {
    [self presentMoviePlayerViewControllerAnimated:tmpMoviePlayViewController]; tmpMoviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile; 
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieViewFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:tmpMoviePlayViewController];
    [tmpMoviePlayViewController.moviePlayer play];
    }
    //[tmpMoviePlayViewController release];
    }
    else{
    MPMoviePlayerController* theMovie=[[[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]] retain];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
    [theMovie play];
    }
    } 
    - (void) setUpForPlay: (NSNotification *) notification {
    UIButton *button = (UIButton *)[self.view viewWithTag:BUTTON_TAG]; // Prepare button for re-starting
    [button setTitle:@"Start" forState:UIControlStateNormal];
    [button setTitle:@"Start" forState:UIControlStateHighlighted];
    [button setBackgroundImage:[[UIImage imageNamed:@"green.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0] forState:UIControlStateNormal];
    [button setBackgroundImage:[[UIImage imageNamed:@"green2.png"] stretchableImageWithLeftCapWidth:12.0 topCapHeight:0.0] forState:UIControlStateHighlighted];
    [button removeTarget:self action:@selector(stopPlayback:) forControlEvents:UIControlEventTouchUpInside];
    [button addTarget:self action:@selector(startPlayback:) forControlEvents: UIControlEventTouchUpInside];
    }
     
  3. thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #3
    Is that something that can be applied in my IBAction I have built already for this?
     
  4. macrumors newbie

    sherry2009

    Joined:
    Dec 14, 2009
    #4
    iphone os4 :MPMoviePlayerController ->MPMoviePlayerViewController
    Code:
    -(void)myMovieFinishedCallback:(NSNotification*)aNotification
    {
        MPMoviePlayerController* theMovie=[aNotification object];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovie];
        [theMovie release];
    	[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
    }
    iphone os 4 should use this:
    Code:
    -(void)myMovieViewFinishedCallback:(NSNotification*)aNotification
    {
        MPMoviePlayerViewController* theMovieView=[aNotification object];
    	[self dismissMoviePlayerViewControllerAnimated];
        [[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:theMovieView];
        [theMovieView release];
    	[[UIApplication sharedApplication] setStatusBarOrientation:UIInterfaceOrientationPortrait animated:YES];
    }
     
  5. thread starter macrumors 6502a

    Joined:
    Jul 30, 2010
    #5
    What about for early versions that aren't iOS4?
     
  6. macrumors newbie

    sherry2009

    Joined:
    Dec 14, 2009
    #6
    Code:
    NSString *path = [[NSBundle mainBundle] pathForResource:@"dream" ofType:@"mp4"];
    	if ([[[UIDevice currentDevice] systemVersion] doubleValue] >= 3.2)
    	{
    		MPMoviePlayerViewController* tmpMoviePlayViewController=[[[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:path]] retain];
    		if (tmpMoviePlayViewController)
    		{			
    			[self presentMoviePlayerViewControllerAnimated:tmpMoviePlayViewController];
    			tmpMoviePlayViewController.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
    			[[NSNotificationCenter defaultCenter] addObserver:self
    													 selector:@selector(myMovieViewFinishedCallback:)
    														 name:MPMoviePlayerPlaybackDidFinishNotification
    													   object:tmpMoviePlayViewController];
    			[tmpMoviePlayViewController.moviePlayer play];
    		}
    		//[tmpMoviePlayViewController release];
    	}else{
    	MPMoviePlayerController* theMovie=[[[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:path]] retain];
        [[NSNotificationCenter defaultCenter] addObserver:self
    											 selector:@selector(myMovieFinishedCallback:)
    												 name:MPMoviePlayerPlaybackDidFinishNotification
    											   object:theMovie];
    	[theMovie play];
    	}
    
     

Share This Page