MPMoviePlayerController stops playing the video af

2019-07-15 11:34发布

问题:

I tried to play a video in iOS but it just played 5s for any video. This is my code:

NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"videoviewdemo" ofType:@"mp4"];
NSURL *movieURL = [[NSURL alloc]initFileURLWithPath:moviePath];

MPMoviePlayerController *theMoviPlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
theMoviPlayer.controlStyle = MPMovieControlStyleFullscreen;
theMoviPlayer.controlStyle = MPMovieControlStyleDefault;
[theMoviPlayer setMovieSourceType:MPMovieSourceTypeFile];
theMoviPlayer.shouldAutoplay = YES;

theMoviPlayer.view.transform = CGAffineTransformConcat(theMoviPlayer.view.transform, CGAffineTransformMakeRotation(M_PI_2));

[theMoviPlayer.view setFrame:self.view.frame];
[theMoviPlayer prepareToPlay];
[self.view addSubview:theMoviPlayer.view];

回答1:

Declare an ivar @property (nonatomic,strong) MPMoviePlayerController *myMovieController;

assign theMoviPlayer to it as follows,

self.myMovieController = theMoviPlayer;
[self.view addSubview: self.myMovieController.view];

and then play the movie.

The issue seems to be that theMoviPlayer is not retained.