切换avaudiosession类别,然后重新控制远程控制中心控件

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

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

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

我试着解释发生了什么:

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

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

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

任何帮助将不胜感激

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

[[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 withOptions:options error:&error]; ... [[AVAudioSession sharedInstance] setActive:YES error:&error] 

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

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

我希望这有帮助。