【问题标题】:How to resume background music after out-going phone call (iOS)?拨出电话后如何恢复背景音乐(iOS)?
【发布时间】:2011-10-27 04:36:43
【问题描述】:

我正在使用 AVAudioPlayer 播放音乐(支持背景)。我的问题是如果我想在听音乐时给某人打电话,通话后如何恢复?我已经实现了 AVAudioPlayer 的委托方法:

- (void)audioPlayerEndInterruption:(AVAudioPlayer *)thePlayer {
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    [self.player play];
}

但这不会让音乐继续播放。

我也尝试过使用 AVAudioSessionDelegate 方法(试一试):

- (void)viewDidLoad {
    [[AVAudioSession sharedInstance] setDelegate:self];
}

- (void)endInterruption
{
    [[AVAudioSession sharedInstance] setActive:YES error:nil];
    [self.player play];
}

但这不会导致音乐继续播放。关于如何解决这个问题的任何想法?

【问题讨论】:

    标签: iphone audio background resume


    【解决方案1】:

    奇怪的是,你说你确实使用了 AVAudioPlayer,为什么你还要实现 AVAudioSessionDelegate?

    而且你对委托方法的实现很糟糕。查看文档

    from AVAudioPlayerDelegate:

    - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player

    - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player withFlags:(NSUInteger)flags

    所以尝试像这样实现它们,它应该可以正常工作

    【讨论】:

    • Marek,我已经实现了你发布的第一个委托方法 - (void)audioPlayerEndInterruption:(AVAudioPlayer *)player,但这不起作用。
    【解决方案2】:

    在 iOS 上,应用程序在后台运行时无法开始播放音频。如果您的应用的音频因前台应用控制了音频会话而停止,则(当前)无法将其取回。

    【讨论】:

      【解决方案3】:

      您可以恢复它。完成音频会话后,您必须通知其他应用程序我的应用程序停用了音频会话,您可以使用它。使用以下代码。

      [[AVAudioSession sharedInstance] setActive:NO withFlags:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation error:&error];
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-03-17
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多