Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 fine when called, but it only plays for four seconds, then a black screen appears. If I tap the screen during playback, it will play the entire sequence. If I stop tapping the screen for four seconds, the black screen appears.

What am i missing?

Kurt


Edited version plays fine.

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];
}
like image 410
Kurt Avatar asked Mar 20 '12 00:03

Kurt


2 Answers

If you're using ARC I believe you need to retain the outer moviePlayer. I just assigned it to a new property myself. HTH

like image 199
davidfrancis Avatar answered Sep 27 '22 21:09

davidfrancis


The solution is that the player would have to be an instance variable or property of the view controller. ie We must use the instance of MPMoviePlayerController

@property (nonatomic,strong) MPMoviePlayerController *myMovieController;

like image 32
Vicky Avatar answered Sep 27 '22 20:09

Vicky