为什么 MPMoviePlayerController 会阻止电影结束后恢复音频?

发布于 2024-09-28 09:22:25 字数 1417 浏览 0 评论 0原文

我有一个类,它使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

沫尐诺 2024-10-05 09:22:25

想通了这一点。事实证明,在 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 than MPMoviePlaybackStateStopped, and in order to make it release the hardware, you have to explicitly call the stop method on MPMoviePlayerController after it finishes playing before trying to resume AVAudioPlayer.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文