views:

429

answers:

1

I have this code trying to run a video on the iPhone 4 Simulator.

The problem is that looks like it loads the player but half a second later loads a back screen on top of the whole application disabling touch and everything, and it looks like it does not play the video also, because I can't hear anything.

Any ideas?!

MPMoviePlayerViewController *mp =
[[MPMoviePlayerViewController alloc] initWithContentURL:videoUrl];

if (mp) {
    mp.moviePlayer.scalingMode = MPMovieScalingModeFill; 
    mp.moviePlayer.movieSourceType = MPMovieSourceTypeFile;
    [mp.moviePlayer play];

    [self presentMoviePlayerViewControllerAnimated:mp];

    [mp release];
}
A: 

I believe the problem is caused by releasing the MPMoviePlayerViewController. Simply retain the controller until you are done with it.

Prior to the "[mp release];" add this line to save the value away.

self.moviePlayerViewController = mp;

Then update your dealloc method to do the release:

- (void)dealloc {
   [_moviePlayerViewController release], _moviePlayerViewController = nil;
   [super dealloc];
}

Add the synthesize to the top of your .m file:

@synthesize moviePlayerViewController = _moviePlayerViewController;

Add the defination to the @interface of your .h file:

MPMovieViewController *_moviePlayerViewController;

Add the property to your .h file:

@property (readwrite, retain) MPMovieViewController *moviePlayerViewController;

You may need some headers in your header:

#import <MediaPlayer/MediaPlayer.h>
#import <MediaPlayer/MPMoviePlayerViewController.h>

You may also need to balance your "presentMoviePlayer" call with the dismiss somewhere:

[self dismissMoviePlayerViewControllerAnimated];

Phew, code everywhere. Anyway, if you are finished with the resource early, you may be able to release it sooner by using NotificationManager to watch for MPMoviePlayerPlaybackDidFinishNotification. There are many examples of that, so I won't repeat it.

Hope this helps.

Dave