ios - 应用程序重新激活后 AVAudioSession 无法播放声音

标签 ios core-audio avaudiosession

所以我有一个音乐应用程序,它使用 AVAudioSession 允许它在后台播放。我用这个电话:

[audioSession setActive:YES
            withOptions:AVAudioSessionSetActiveOptionNotifyOthersOnDeactivation
                  error:nil];

我现在的问题是如果我转到另一个应用程序并且它窃取了 Audio Session (因此现在停止从我的应用程序播放音乐并播放其他东西),然后我回到我的应用程序,无论我做什么来重置我的 Audio Session 或我的音频单元,我的应用程序的声音消失了。

有人知道怎么办吗?

最佳答案

因此在注册 AVAudioSession 通知后:

[[NSNotificationCenter defaultCenter] addObserver:self
                                         selector:@selector(handleAudioSessionInterruption:)
                                             name:AVAudioSessionInterruptionNotification
                                           object:aSession]; 

您需要恢复/重新启动您需要在处理程序中断类型中重新启动播放器是 AVAudioSessionInterruptionTypeEnded:

- (void)handleAudioSessionInterruption:(NSNotification*)notification {

    NSNumber *interruptionType = [[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey];
    NSNumber *interruptionOption = [[notification userInfo] objectForKey:AVAudioSessionInterruptionOptionKey];

    switch (interruptionType.unsignedIntegerValue) {
        case AVAudioSessionInterruptionTypeBegan:{
            // • Audio has stopped, already inactive
            // • Change state of UI, etc., to reflect non-playing state
        } break;
        case AVAudioSessionInterruptionTypeEnded:{
            // • Make session active
            // • Update user interface
            // • AVAudioSessionInterruptionOptionShouldResume option
            if (interruptionOption.unsignedIntegerValue == AVAudioSessionInterruptionOptionShouldResume) {
                // Here you should continue playback.
                [player play];
            }
        } break;
        default:
            break;
    }
}

您可以在这里看到完整的解释:AVplayer resuming after incoming call

关于ios - 应用程序重新激活后 AVAudioSession 无法播放声音,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26196533/

相关文章:

ios - 如何将 AVAsset 音频从一台 iOS 设备无线传输到另一台?

ios - 锁屏上的 AVAudioPlayer

core-audio - Audio Session "Ducking"在 iOS 4 中损坏...?

ios - 使用汉堡菜单时访问另一个 View Controller 中的变量时出现问题

ios - 用 Swift 初始化 UIViewController 的子类?

c# - 绑定(bind)项目找不到类型 Basetype 属性

ios - Inter App Audio 技术 : make effect node and instrument node independent

ios - 通过蓝牙命令控制AVAudioRecorder

ios - 应用程序在静音模式下播放声音-Xcode

ios - 如何在 Swift 中为每个 UI 测试断言从空核心数据开始?