iOS在视图中播放背景视频

时间:2012-06-11 20:30:00

标签: ios objective-c video ios5 playback

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

1 个答案:

答案 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];