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]; }
ios objective-c mpmovieplayercontroller
Kurt
source share