你如何在iPhone应用程序中播放电影?

时间:2011-04-27 00:31:18

标签: iphone xcode media-player movie

我想在我的iphone应用程序的开头添加一部电影。我看了教程,这是我提出的代码。为什么它不起作用。我甚至没看电影。我使用以下代码从我的ViewController的viewDidLoad函数调用它:

NSString *path = [[NSBundle mainBundle] pathForResource:@"BTS Intro" ofType:@"mov"];
[self playMovieAtURL:[NSURL fileURLWithPath:path

-(void)playMovieAtURL:(NSURL *)theURL {
MPMoviePlayerController *thePlayer = [[MPMoviePlayerController alloc] initWithContentURL:theURL];
thePlayer.scalingMode = MPMovieScalingModeAspectFill;
thePlayer.controlStyle = MPMovieControlStyleNone;

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(myMovieFinishedCallback:) name:MPMoviePlayerPlaybackDidFinishNotification object:thePlayer];
[thePlayer play];
}

-(void)myMovieFinishedCallback:(NSNotification *)aNotification {
MPMoviePlayerController *thePlayer = [aNotification object];
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:thePlayer];
[thePlayer release];
}

1 个答案:

答案 0 :(得分:2)

您开始播放电影但未将[thePlayer视图]添加到视图层次结构中,这就是您没有看到任何内容的原因。在Apple docs中查看此操作的描述:

将电影播放器​​的视图添加到应用的视图层次结构时,请务必正确调整帧的大小,如下所示:

MPMoviePlayerController *player =
        [[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player.view setFrame: myView.bounds];  // player's frame must match parent's
[myView addSubview: player.view];
// ...
[player play];

MPMoviePlayerController Class Reference