AVAudioSession类别无法按文档要求运行 [英] AVAudioSession category not working as documentation dictates

查看:92
本文介绍了AVAudioSession类别无法按文档要求运行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个iOS应用,该应用在某些位置具有一些音频反馈,但是我希望允许用户在后台播放的任何其他音乐在此播放.另外,我希望我的应用中的音频尊重静音开关.根据开发人员文档,应通过AVAudioSession环境类别全部启用此功能.这是我正在使用的代码:

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;
}

代码执行得很好,的确确实可以使应用程序的声音通过iPod音乐播放.但是,它没有执行的操作是尊重静音开关.我尝试将这段代码换成类似的C音频调用(诸如AudioSessionSetProperty之类的东西),而不是Objective-C调用,但是我得到了相同的结果-环境会话类别根本不想尊重静音开关,尽管文档说应该这样做.

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 :)

推荐答案

我认为我可以解决这个问题-事实证明,它与我的应用程序完全无关,而与iPod应用程序无关.当不播放iPod时,我的应用程序应遵守静音开关的要求,然后允许iPod播放它-我想要的所有行为.但是,当iPod播放时,应用程序停止响应静音开关,因此我认为这只是iPod对设备音频设置所做的事情.如果我真的想花时间在上面,我可能可以解决它,但是只要在iPod不播放时它遵守静音开关,对我来说就足够了.

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.

要解决此问题,只需使用此功能来确定静音开关是否手动打开,并且如果结果为是",则不播放声音.但是,如果您没有中央音频管理器课程,可能会有些痛苦.如果Apple可以在其文档中发布此行为,那就太好了.

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);
}

这篇关于AVAudioSession类别无法按文档要求运行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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