MPMoviePlayerController stops after four seconds

I am trying to set up a very simple video player. (iOS 5.1, Xcode 4.3.1)

-(void)playMedia { NSString *movieFile = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"]; MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:movieFile]]; [moviePlayer prepareToPlay]; moviePlayer.view.frame = self.view.bounds; moviePlayer.scalingMode = MPMovieScalingModeAspectFit; moviePlayer.movieSourceType = MPMovieSourceTypeFile; moviePlayer.fullscreen = YES; moviePlayer.controlStyle = MPMovieControlStyleFullscreen; [self.view addSubview: moviePlayer.view]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playMediaFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; [moviePlayer play]; } 

It works great on a call, but it only plays for four seconds, then a black screen appears. If I touch the screen during playback, it will play the entire sequence. If I stop tapping the screen for four seconds, a black screen will appear.

What am I missing?

Kurt


The edited version works great.

In the interface file:

 @property (nonatomic,strong) MPMoviePlayerController *myMovieController; 

In the .m file:

 -(void)playMedia { NSString *movieFile = [[NSBundle mainBundle] pathForResource:@"Movie" ofType:@"m4v"]; MPMoviePlayerController *moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:[NSURL fileURLWithPath:movieFile]]; [moviePlayer prepareToPlay]; moviePlayer.view.frame = self.view.bounds; moviePlayer.scalingMode = MPMovieScalingModeAspectFit; moviePlayer.movieSourceType = MPMovieSourceTypeFile; moviePlayer.fullscreen = YES; moviePlayer.controlStyle = MPMovieControlStyleFullscreen; self.myMovieController = moviePlayer; [self.view addSubview: self.myMovieController.view]; [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(playMediaFinished:) name:MPMoviePlayerPlaybackDidFinishNotification object:moviePlayer]; [self.myMovieController play]; } 
+8
ios objective-c mpmovieplayercontroller
source share
2 answers

If you use ARC, I believe that you need to save the external MoviePlayer. I just assigned it to the new property. NTN

+19
source share

The solution is that the player must have an instance variable or a view controller property. those. we must use an instance of MPMoviePlayerController

@property (non-atomic, strong) MPMoviePlayerController * myMovieController;

+4
source share

All Articles