为什么 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];
}
}
恢复工作正常,无需重新创建 AVAudioPlayer
对象(即 play
方法)在 3.2 及以下操作系统版本上的类似代码上返回 YES)。但在 iOS4 上,play
方法始终返回 NO,并且必须重新创建对象。为什么会这样,我可以正确恢复后台轨道吗(我有上面使用的解决方案不可接受的情况。)?
I have a class that plays a repeating background music loop with an AVAudioPlayer
, and on specific occasion, plays a full-screen video with its own sound track using MPMoviePlayerController
. In order to to have only one track at a time, I stop the background music before launching the video:
-(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];
}
}
The resumption worked fine without recreating the AVAudioPlayer
object (i.e. the play
method returned YES) on analogous code on os versions up to and including 3.2. But on iOS4, the play
method always returns NO, and has to recreate the object. Why is that, and can I get to resume the background track properly (I have cases where the solution used above is unacceptable.)?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
想通了这一点。事实证明,在 iOS 3.2 及更高版本中,当视频播放完毕时,它会进入
MPMoviePlaybackStatePaused
状态,而不是MPMoviePlaybackStateStopped
状态,并且为了使其释放硬件,您需要必须在播放完成后显式调用 MPMoviePlayerController 上的stop
方法,然后才能尝试恢复AVAudioPlayer
。Figured this out. It turns out that in iOS 3.2 and above, when a video finishes playing, it goes into
MPMoviePlaybackStatePaused
state rather thanMPMoviePlaybackStateStopped
, and in order to make it release the hardware, you have to explicitly call thestop
method on MPMoviePlayerController after it finishes playing before trying to resumeAVAudioPlayer
.