我已将此代码添加到viewDidLoad
方法:
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"myvid" ofType:@"m4v"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
MPMoviePlayerController* moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayer.controlStyle = MPMovieControlStyleNone;
[self.view insertSubview: moviePlayer.view aboveSubview: self.view];
[moviePlayer play];
它不起作用!如何在iOS中的视图背景中播放视频? (使用故事板)
答案 0 :(得分:2)
看起来您创建的MPMoviePlayerController实例在播放过程中受到ARC的伤害。
尝试制作MPMoviePlayerController的强属性,并在开始播放前分配您在那里创建的实例。
所以你的代码看起来像这样
NSBundle *bundle = [NSBundle mainBundle];
NSString *moviePath = [bundle pathForResource:@"myvid" ofType:@"m4v"];
NSURL *movieURL = [NSURL fileURLWithPath:moviePath];
self.moviePlayer = [[MPMoviePlayerController alloc] initWithContentURL:movieURL];
self.moviePlayer.controlStyle = MPMovieControlStyleNone;
//set the frame of movie player
self.moviePlayer.view.frame = CGRectMake(0, 0, 200, 200);
[self.view addSubView:self.moviePlayer.view];
[self.moviePlayer play];