我有一个类使用AVAudioPlayer
播放重复的背景音乐循环,在特定场合,使用MPMoviePlayerController
播放带有自己声道的全屏视频。为了一次只有一首曲目,我会在启动视频之前停止背景音乐:
-(void)startVideo{
[backgroundMusic stop];
MPMoviePlayerViewController *mp = [[MPMoviePlayerViewController alloc] initWithContentURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"videofile" ofType:@"m4v"]]];
[self presentMoviePlayerViewControllerAnimated:mp];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(videoOver) name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
[mp.moviePlayer play];
[mp release];
}
-(void)videoOver{
[[NSNotificationCenter defaultCenter] removeObserver:self name:MPMoviePlayerPlaybackDidFinishNotification object:nil];
if(![backgroundMusic play]){
NSLog(@"bad: can't resume bg music!");
[backgroundMusic release];
backgroundMusic = [[AVAudioPlayer alloc] initWithContentsOfURL:[NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"videofile" ofType:@"m4v"]] error:NULL];
backgroundMusic.delegate = self;
backgroundMusic.numberOfLoops = -1;
[backgroundMusic play];
}
}
恢复正常工作没有重新创建AVAudioPlayer
对象(即play
方法返回YES)对os版本(包括3.2)的类似代码。但是在iOS4上,play
方法总是返回NO,并且必须重新创建对象。为什么会这样,我能否正确恢复背景音轨(我有上面使用的解决方案是不可接受的情况。)
答案 0 :(得分:2)
想出来了。事实证明,在iOS 3.2及更高版本中,当视频完成播放时,它会进入MPMoviePlaybackStatePaused
状态而不是MPMoviePlaybackStateStopped
,并且为了使其释放硬件,您必须显式调用{在尝试恢复stop
之前完成播放后,MPMoviePlayerController上的{1}}方法。