PDA

View Full Version : MPMoviePlayerController for iPhone under iOS4, iOS4, and iPad




newtoiphonesdk
Aug 17, 2010, 11:18 AM
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
- (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?



sherry2009
Aug 18, 2010, 10:55 PM
-(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];
}

newtoiphonesdk
Aug 19, 2010, 01:32 AM
-(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?

sherry2009
Aug 19, 2010, 04:51 AM
iphone os4 :MPMoviePlayerController ->MPMoviePlayerViewController
-(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:
-(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];
}

newtoiphonesdk
Aug 19, 2010, 11:02 AM
What about for early versions that aren't iOS4?

sherry2009
Aug 19, 2010, 09:30 PM
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];
}