我正在尝试播放一部介绍电影(如启动画面)。它曾经适用于sdk 3.0,但现在我正在为iPad做它。并且它没有运行电影,而只是在视图出现之前显示黑屏。 这是我的代码
(BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
NSString *moviePath = [[NSBundle mainBundle] pathForResource:@"intro" ofType:@"m4v" inDirectory:nil];
NSURL *movieURL = [[NSURL fileURLWithPath:moviePath] retain];
MPMoviePlayerController *moviePlayer=[[MPMoviePlayerController alloc] initWithContentURL:movieURL];
moviePlayer.fullscreen=YES;
moviePlayer.view.frame = CGRectMake(0, 0,1024,768);
[moviePlayer setShouldAutoplay:YES];
[moviePlayer setControlStyle:MPMovieControlStyleNone];
[moviePlayer setRepeatMode:MPMovieRepeatModeNone];
[moviePlayer prepareToPlay];
[moviePlayer play];
[[NSNotificationCenter defaultCenter] addObserver:self
selector:@selector(moviePlaybackDidFinish:)
name:MPMoviePlayerPlaybackDidFinishNotification
object:moviePlayer];
return YES;
}
这是我的moviePlaybackDidFinish
- (void) moviePlaybackDidFinish:(NSNotification*)notification
{
MPMoviePlayerController *theMovie = [notification object];
[[NSNotificationCenter defaultCenter] removeObserver:self
name:MPMoviePlayerPlaybackDidFinishNotification
object:theMovie];
theMovie.initialPlaybackTime = -1;
[theMovie pause];
[theMovie stop];
[theMovie release];
[window addSubview:viewController.view];
[window makeKeyAndVisible];
}
我不知道我在这里失踪了什么? 谢谢你的帮助...... 更新我刚刚发现它播放声音但没有视频....这是显而易见的吗?请帮忙
答案 0 :(得分:0)
当您使用MPMoviePlayerController
而不是MPMoviePlayerViewController
时,我认为您需要在调用播放之前将播放器的视图添加到视图层次结构中,根据文档:
[player.view setFrame: myView.bounds]; // player's frame must match parent's
[myView addSubview: player.view]; //add the player' view
这可以解释为什么你听到音频但没有看到任何东西,非常确定你也需要首先设置应用程序窗口([window makeKeyAndVisible];
等)