OGeek|极客世界-中国程序员成长平台

标题: ios - 切换 avaudiosession 类别然后重新控制远程控制中心控件 [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 10:41
标题: ios - 切换 avaudiosession 类别然后重新控制远程控制中心控件

这是我第一次发帖问一个问题,因为我通常不需要帮助,但我不知道这是否可能。我需要的是在这两类avaudiosession之间切换
当应用程序从允许混音切换到不混音时,请收回控制中心 Remote 的控制权。

  • [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers 错误:nil]


  • [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:nil error:nil]

  • 我会尝试解释发生了什么:

    它们都独立工作,所以如果我从第一个 avaudiosession 配置开始,它允许混合并将控制中心的 Remote 正确切换到 iPod。

    如果我启动第二个 avaudiosession 配置,应用程序会正确控制控制中心的 Remote 。

    当我尝试切换这些选项时会出现问题。关闭混音后,当我切换应用程序时,不会重新控制 Remote 。

    任何帮助将不胜感激



    Best Answer-推荐答案


    我找到了一个适合我的解决方案,其中涉及调用

    [[UIApplication sharedApplication] beginReceivingRemoteControlEvents]
    

    或者
    [[UIApplication sharedApplication] endReceivingRemoteControlEvents]
    

    在设置 AVAudioSession 类别选项之前。例如:
    NSUInteger options = ... // determine your options
    
    // it seems that calls to beginReceivingRemoteControlEvents and endReceivingRemoteControlEvents
    // need to be balanced, so we keep track of the current state in _isReceivingRemoteControlEvents
    
    BOOL shouldBeReceivingRemoteControlEvents = ( 0 == (options & AVAudioSessionCategoryOptionMixWithOthers) );
    
    if(_isReceivingRemoteControlEvents != shouldBeReceivingRemoteControlEvents) {
        if(shouldBeReceivingRemoteControlEvents) {
            [[UIApplication sharedApplication] beginReceivingRemoteControlEvents];
            _isReceivingRemoteControlEvents=YES;
        } else {
            [[UIApplication sharedApplication] endReceivingRemoteControlEvents];
            _isReceivingRemoteControlEvents=NO;
        }
    }
    
    [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptionsptions error:&error];
    
    ...
    
    [[AVAudioSession sharedInstance] setActive:YES error:&error]
    

    通过使用一个变量来跟踪应用程序当前是否正在接收远程控制事件,我已经能够获得一致的结果,以便我可以确保对 (begin/end)ReceivingRemoteControlEvents 的调用是平衡的。我没有找到任何说明您需要这样做的文档,但除此之外,事情似乎并不总是按预期运行,特别是因为我在整个应用程序过程中多次调用此代码。

    在我的实现中,每次应用程序进入前台以及每次开始播放音频之前都会调用上面的代码。

    我希望这有帮助。

    关于ios - 切换 avaudiosession 类别然后重新控制远程控制中心控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24625510/






    欢迎光临 OGeek|极客世界-中国程序员成长平台 (http://ogeek.cn/) Powered by Discuz! X3.4