gpt4 book ai didi

ios - 切换音频类别然后重新控制远程控制中心控件

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:04:51 25 4
gpt4 key购买 nike

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

  1. [[AVAudioSession sharedInstance] setCategory:AVAudioSessionCategoryPlayback withOptions:AVAudioSessionCategoryOptionMixWithOthers 错误:无]

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

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

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

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

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

任何帮助将不胜感激

最佳答案

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

[[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 的调用是平衡的。我没有找到任何说明您需要这样做的文档,但除此之外,事情似乎并不总是按预期运行,特别是因为我在整个应用程序过程中多次调用此代码。

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

希望对您有所帮助。

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com