2

我已将此代码添加到该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 的视图背景中播放视频?(使用故事板)

4

1 回答 1

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];
于 2013-03-25T17:52:04.007 回答