Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

newtoiphonesdk

macrumors 6502a
Original poster
Jul 30, 2010
567
2
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?
 
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];
}
 
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];
}
Is that something that can be applied in my IBAction I have built already for this?
 
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];
}
 
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];
	}
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.