AVAudioPlayer ,并在特定情况下,使用 MPMoviePlayerController . 为了一次只能播放一首曲目,我在启动视频之前停止背景音乐:
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]; } }
恢复工作进行得很好,没有重新创建 对象(即 play 玩
play
玩
明白了。事实证明,在iOS3.2及以上版本中,视频播放结束后,会进入 MPMoviePlaybackStatePaused 国家而不是 MPMoviePlaybackStateStopped ,为了使它释放硬件,必须显式调用 stop AVAudioPlayer .
MPMoviePlaybackStatePaused
MPMoviePlaybackStateStopped
stop