AVAudioSession 类别未按照文档指示工作

发布于 2024-10-30 14:04:53 字数 603 浏览 5 评论 0原文

我有一个 iOS 应用程序,在某些地方有一些音频反馈,但我希望允许用户在后台播放的任何其他音乐都可以在此之上播放。此外,我希望应用程序中的音频遵守静音开关。根据开发人员文档,此功能应全部由 AVAudioSession 环境类别启用。这是我正在使用的代码:

if (!hasInitialisedAudioSession) {
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryAmbient error:NULL];

    [session setActive:YES error:NULL];

    hasInitialisedAudioSession = YES;
}

代码执行得很好,并且它确实让应用程序的声音通过 iPod 音乐播放。然而,它不尊重静音开关。我尝试将此代码替换为类似的 C 音频调用(例如 AudioSessionSetProperty 之类的东西)而不是 Objective-C 调用,但我得到了相同的结果 - 环境会话类别根本不想尊重静音开关,尽管文档说它应该这样做。

有什么想法吗?感谢您的帮助:)

I have an iOS app that has some audio feedback in certain places, but I want any other music the user has playing in the background to be allowed to play over this. In addition, I want the audio in my app to respect the mute switch. According to the developer documentation, this functionality should all be enabled by the AVAudioSession ambient category. This is the code I'm using:

if (!hasInitialisedAudioSession) {
    AVAudioSession *session = [AVAudioSession sharedInstance];
    [session setCategory:AVAudioSessionCategoryAmbient error:NULL];

    [session setActive:YES error:NULL];

    hasInitialisedAudioSession = YES;
}

The code is executing just fine, and it does indeed let the app sounds play over iPod music. What it doesn't do, however, is respect the mute switch. I've tried swapping this code out for similar C audio calls (stuff like AudioSessionSetProperty) instead of the Objective-C calls, but I get the same result - the ambient session category simply doesn't want to respect the mute switch, despite what the documentation says it should be doing.

Any ideas? Thanks for the help :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

银河中√捞星星 2024-11-06 14:04:53

我想我设法解决了这个问题 - 结果发现它与我的应用程序完全无关,而是与 iPod 应用程序有关。当 iPod 不播放时,我的应用程序遵循静音开关,然后允许 iPod 继续播放 - 这都是我想要的行为。然而,当 iPod 正在播放时,应用程序停止响应静音开关,所以我认为这只是 iPod 对设备音频设置所做的事情。如果我真的想花时间在它上面,我可能可以想办法解决这个问题,但只要它在 iPod 不播放时遵循静音开关,这对我来说就足够了。

编辑:要解决此问题,只需使用此功能来确定静音开关是否手动打开,如果结果为“是”,则不要播放声音。不过,如果您没有中央音频管理器类,可能会有点痛苦。如果 Apple 能够在其文档中发布此行为,那就太好了。

- (BOOL)deviceIsSilenced
{
    #if TARGET_IPHONE_SIMULATOR
    // return NO in simulator. Code causes crashes for some reason.
    return NO;
    #endif

    CFStringRef state;
    UInt32 propertySize = sizeof(CFStringRef);
    AudioSessionInitialize(NULL, NULL, NULL, NULL);
    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);

    return (CFStringGetLength(state) <= 0);
}

I think I managed to work it out - turns out that it has nothing to do with my app at all, but rather the iPod app. My app obeys the mute switch as it should when the iPod isn't playing, and then allows the iPod to play over it - all behaviour I wanted. However, when the iPod is playing, the app stops responding to the mute switch, so I think it's just something the iPod does to the device audio settings. I could probably work a way around it if I really wanted to spend the time on it, but as long as it obeys the mute switch when the iPod isn't playing that's good enough for me.

EDIT: to work around this, just use this function to determine whether or not the mute switch is on manually, and don't play your sounds if the result is YES. Could be a bit of a pain if you don't have a central audio manager class, though. It would be nice if Apple could publish this behaviour in their documentation.

- (BOOL)deviceIsSilenced
{
    #if TARGET_IPHONE_SIMULATOR
    // return NO in simulator. Code causes crashes for some reason.
    return NO;
    #endif

    CFStringRef state;
    UInt32 propertySize = sizeof(CFStringRef);
    AudioSessionInitialize(NULL, NULL, NULL, NULL);
    AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &propertySize, &state);

    return (CFStringGetLength(state) <= 0);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文