如何在iPhone应用程序中播放电影?
问题描述:
我想在我的iPhone应用程序开始处添加一部电影。我看过这些教程,这是我提出的代码。为什么它不工作。我什至不看电影。我把它从我的ViewController与下面的代码viewDidLoad中功能:如何在iPhone应用程序中播放电影?
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];
}
答
您发起了电影的播放,但没有添加[thePlayer视图]到视图层次,这就是为什么你看不到任何东西。看看这个操作的描述苹果文档:
当您添加的电影播放器的看法到应用的视图层次,一定要尺寸框架正确,如下所示:
MPMoviePlayerController *player =
[[MPMoviePlayerController alloc] initWithContentURL: myURL];
[player.view setFrame: myView.bounds]; // player's frame must match parent's
[myView addSubview: player.view];
// ...
[player play];
+0
myView在哪里设置? – Talon 2012-02-01 00:18:22
路径绝对正确吗?它是区分大小写的,所以电影需要放在您的项目中,并准确命名为“BTS Intro.mov”。 – 2011-04-27 00:52:43