首页 文章

检测AVAudioSession何时被“剔除”

提问于
浏览
16

我正在开发一个Podcast应用程序 . 这将使用 AVAudioSessionCategoryPlayback 会话类型播放音频 . 这几乎可以完成我想要它做的所有事情 . 它会暂停其他播放的音乐,当有来电时它会被打断,并且通常可以按照我想要的方式正常工作 .

one 例外 . 当播放更高优先级的音频时(例如,当运行转弯导航应用程序时),我的应用程序中的音频会在音量上下降但继续运行 . 这造成了两种不希望的声音的可怕混合 . 我宁愿选择我的应用程序中的音频在过度播放的声音中暂停,然后在完成后再次继续 .

我已经尝试更改不同的AVAudioSession属性,例如 AVAudioSessionCategoryOptionMixWithOthers ,但这些只会改变低优先级声音与我的应用程序混合的方式 . 它们不会改变我的如何与更高优先级的声音混合 . 我也尝试过观察 sharedSessionotherAudioPlaying 属性,但是当这些短片被覆盖时,这并没有引发变化 .

有没有办法检测我的应用程序中的音频何时被躲避,以便我可以暂停它?或者,为了防止我的应用程序的音频被躲避,以便将这些其他声音视为中断?

谢谢 .

6 回答

  • 0

    在您 AppDelegate.m 类和 didFinishLaunchingWithOptions 函数中,请听取以下通知

    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(audioSessionInterruption:) name:AVAudioSessionInterruptionNotification object:nil];
    

    在应用程序启动方法之外,将选择器实现为

    - (void)audioSessionInterruption:(NSNotification *)notif{
        NSLog(@"\n\nInterruption Notification :%@\n\n",notif.userInfo);
    }
    

    希望这对你有用....

  • 1

    我写了关于这个问题的Apple DTS . 这是我得到的:

    感谢您联系Apple开发者技术支持(DTS) . 我们的工程师已经审核了您的请求,并得出结论,鉴于当前出货的系统配置,没有支持的方法来实现所需的功能 . 如果您希望Apple将来考虑添加支持以允许/通知后台应用程序停止播放音频,前台应用程序选择删除其他音频,然后启用后台应用程序以在完成后再次启动音频,请提交通过http://bugreport.apple.com上的Bug Reporter工具进行增强请求 .

    现在不幸的是,这似乎是不可能的 .

  • 0

    此功能已在iOS 9中添加,可以通过将音频会话模式设置为AVAudioSessionModeSpokenAudio来启用 . 其他应用程序(例如导航应用程序)会中断您的音频而不是躲避 .

    见:https://developer.apple.com/documentation/avfoundation/avaudiosessionmodespokenaudio

  • 0

    我从来没有这样做,但我想你可以使用AudioSession服务C函数audioSessionAddPropertyListener为kAudioSessionProperty_OtherAudioIsPlaying注册你的回调 .

    请注意,“FLATDACTED”文档和Apple DevForums中存在一些混淆,即“未来”所有或部分AudioSession C函数被弃用的可能性 .

  • 1

    您可能希望查看中断,并在中断时丢弃音频会话,然后在中断结束时恢复,如下所示:

    在ViewDidLoad中:

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
    
    if ([self canBecomeFirstResponder]) {
        [self becomeFirstResponder];
    }
    

    处理通知:

    static NSInteger audioSessionActiveCounter = 0;
    
    - (void)audioSessionDidChangeInterruptionType:(NSNotification *)notification
    {
        AVAudioSessionInterruptionType interruptionType = [[[notification userInfo] objectForKey:AVAudioSessionInterruptionTypeKey] unsignedIntegerValue];
    
        if (AVAudioSessionInterruptionTypeBegan == interruptionType)
        {
           DDLogVerbose(@"Session interrupted: --- Begin Interruption ---");
           // stop your session here with a setActive:NO
    
        }
        else if (AVAudioSessionInterruptionTypeEnded == interruptionType)
        {
            DDLogVerbose(@"Session interrupted: --- End Interruption ---");
           // resume your session here with a setActive:YES
        }
    }
    

    希望能帮助到你 .

  • 1

    我不相信kAudioSessionProperty_OtherAudioIsPlaying应该以任何方式回调 - 可能是你必须定期轮询这个值 .

    当在设备上播放其他音频时,它应返回非零值 . 这可能并不表示您本身就“被躲避”,但这表明其他音频正在与您的音频混合 . 据说,只有当另一个应用程序设置其音频会话的kAudioSessionProperty_OtherMixableAudioShouldDuck属性时,您才会被躲避 .

    关于民意调查 - 是的,它可能不像回调那样优雅,但有时候这是必要的,我怀疑轮询这个值是一个小问题 .

    希望这可以帮助 .

相关问题