制作MPMoviePlayerController主视图

时间:2012-08-30 16:28:11

标签: objective-c mpmovieplayercontroller

我有一个水平滚动视图,如Springboard,每个页面上都有16个按钮。当您单击按钮时,我只想看到播放视频的MPMoviePlayerController而不是其他内容,就像您正在视频应用程序中观看电影一样。因此,我有以下代码:

- (void)button: (id) sender {
    NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"video" ofType:@"MOV"];
    NSURL *videoURL = [NSURL fileURLWithPath:moviePath];
    MPMoviePlayerController *moviePlayerController = [[MPMoviePlayerController alloc] initWithContentURL:videoURL];

    [[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(moviePlaybackComplete:)
                                             name:MPMoviePlayerPlaybackDidFinishNotification
                                           object:moviePlayerController];

    moviePlayerController.fullscreen = YES;

    [self.view addSubview:moviePlayerController.view];
    [moviePlayerController play];
}

- (void)moviePlaybackComplete:(NSNotification *)notification
{
    MPMoviePlayerController *moviePlayerController = [notification object];
    [[NSNotificationCenter defaultCenter] removeObserver:self
                                                name:MPMoviePlayerPlaybackDidFinishNotification
                                              object:moviePlayerController];

    [moviePlayerController.view removeFromSuperview];
    [moviePlayerController release];
}

不幸的是,我只能听到声音并且没有视频显示。请问有人帮帮我吗?

更新:我发现你必须制作一个框架。我使用了以下代码:

[moviePlayerController.view setFrame:CGRectMake(0, 0, 768, 1004)];

但这并不是全屏。即使我将全屏属性设置为YES。我可以看到,因为视图顶部没有视频控件,但在视图的底部。

1 个答案:

答案 0 :(得分:0)

我找到了解决方案。您应该使用以下代码:

NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"1" ofType:@"MOV"];
NSURL *videoURL = [NSURL fileURLWithPath:moviePath];
MPMoviePlayerViewController *playerView = [[MPMoviePlayerViewController alloc] initWithContentURL:videoURL];

[self presentModalViewController:playerView animated:YES];