我正在尝试从视图控制器播放电影。我正在使用下面的代码来做到这一点:

VideoPlayerViewController* moviePlayer = [self.storyboard instantiateViewControllerWithIdentifier:@"videoPlayerController"];
moviePlayer.view.frame = rect;
UIWindow *window = [[[UIApplication sharedApplication] windows] lastObject];
[window.rootViewController presentViewController:moviePlayer animated:NO completion:nil];
[moviePlayer playMoviesForItems:items];


通常情况下,它工作正常。但是有时候,电影开始了,但是我看不到视频,只能听见声音。

在一切之上播放视频的最佳方法是什么?

最佳答案

NSString *path = [[NSBundle mainBundle] pathForResource:@"myVideo" ofType:@"mp4"];

MPMoviePlayerController *myPlayer = [[MPMoviePlayerController alloc] init];
myPlayer.shouldAutoplay = YES;
myPlayer.repeatMode = MPMovieRepeatModeOne;
myPlayer.fullscreen = YES;
myPlayer.movieSourceType = MPMovieSourceTypeFile;
myPlayer.scalingMode = MPMovieScalingModeAspectFit;
myPlayer.contentURL =[NSURL fileURLWithPath:path];
[self.view addSubview:myPlayer.view];
[myPlayer play];

另一种解决方案是将影片锁定到另一个View Controller,并在影片结束时将其关闭。

10-08 01:02