【问题标题】:Switching avaudiosession categories then retaking control of remote control center controls切换 avaudiosession 类别,然后重新控制远程控制中心控件
【发布时间】:2014-08-28 19:35:09
【问题描述】:

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

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

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

我会试着解释发生了什么:

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

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

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

任何帮助将不胜感激

【问题讨论】:

标签: ios xcode controls avaudiosession


【解决方案1】:

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

[[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]

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

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

我希望这会有所帮助。

【讨论】:

  • 它似乎对我不起作用。我的意思是,如果您从仅播放不混音开始,然后运行您的代码,它会切换到混音模式(这意味着我的应用程序不再控制控制中心)。但是,如果我尝试切换回播放并控制控制中心,它就不起作用。除非您使用其他代码切换回来。
  • 为我完成这项工作的关键是在更改 AVAudioSession 类别选项之前适当地调用 beginReceivingRemoteControlEvents 或 endReceivingRemoteControlEvents。你试过这样做吗?跟踪您是否应该调用 begin/end 以避免在您已经接收远程控制事件时调用 begin... 或调用 end... 而您不是,这也很重要。我希望这是有道理的,我试图在答案中详细解释。
  • 我让它工作了,你的建议是让它工作的关键。那就是跟踪 beginReceiving 和 endReceiving。但是,要让我的工作 100% 的时间需要 [[AVAudioSession sharedInstance] setCategory:nil error:nil];在创建会话之前。
  • 嗨,马修,您能否详细说明您的解决方案?我正在尝试做同样的事情,但没有成功。我可以毫无问题地从 AVAudioSessionCategoryPlayback 转到 AVAudioSessionCategoryOptionMixWithOthers。但我不能反其道而行之。我的意思是,一旦我失去对 Apple Music 控制中心的控制权,我就无法重新获得它,除非我杀死我的应用程序。
  • 我偶然发现了这个,只是发现了一个我之前解决并回答过的类似问题。从最新的 iOS 10 测试版开始,答案仍然有效。 stackoverflow.com/a/32284816/924021.
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-05-19
  • 1970-01-01
  • 1970-01-01
  • 2011-06-05
  • 1970-01-01
相关资源
最近更新 更多