切换avaudiosession类别,然后重新控制远程控制中心控件 [英] Switching avaudiosession categories then retaking control of remote control center controls

查看:103
本文介绍了切换avaudiosession类别,然后重新控制远程控制中心控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我提出问题的第一篇帖子,因为我从来不需要帮助,但我无法弄清楚这是否可行。我需要的是在这两类avaudiosession
之间切换,当切换是由混合允许到没有混合时,应用程序收回控制中心遥控器的控制权。

This is my first post asking a question as i never usually need help but i can't figure out if this is even possible. What i need is to switch between these two categories of avaudiosession and when the switch is made from mixing allowed to no mixing for the app take back control of the remote controls in the control center.


  1. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers error:nil]


  1. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:nil error:nil]

我试着解释发生了什么:

Ill try explain what is occurring:

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

They both work independently so if i start with the first avaudiosession config it allows mixing and correctly switches the remote controls in the control center to iPod.

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

And if i start the second avaudiosession config the app correctly takes control of the remote control in the control center.

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

The issue occurs when i trying toggle these options. When i toggle the app doesn't retake control of the remote controls after mixing is turned off.

任何帮助将不胜感激

推荐答案

我找到了一个适合我的解决方案,包括致电

I've found a solution that works for me, which involves calling

[[UIApplication sharedApplication] beginReceivingRemoteControlEvents]

[[UIApplication sharedApplication] endReceivingRemoteControlEvents]

在设置AVAudioSession类别选项之前。例如:

before setting AVAudioSession category options. eg:

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 withOptions:options error:&error];

...

[[AVAudioSession sharedInstance] setActive:YES error:&error]

我已经能够通过使用变量来跟踪应用程序当前是否正在接收远程控制事件来实现一致的结果,这样我就可以确保调用(开始) / end)ReceivingRemoteControlEvents是平衡的。我没有找到任何文档说明你需要这样做但是事情似乎并不像预期的那样,特别是因为我在整个应用程序过程中多次调用这段代码。

I've been able to achieve consistent results by using a variable to keep track of whether or not the app is currently receiving remote control events so that I can ensure that calls to (begin/end)ReceivingRemoteControlEvents are balanced. I haven't found any documentation that says that you need to do this but otherwise things don't always seem to behave as expected, particularly since I call this code multiple times throughout the course of the application.

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

In my implementation, the code above gets called each time the app comes to the foreground and also just before each time I begin playing audio.

我希望这会有所帮助。

这篇关于切换avaudiosession类别,然后重新控制远程控制中心控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆