简体   繁体   English

AVAudioSession类别无法按文档要求运行

[英]AVAudioSession category not working as documentation dictates

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. 我有一个iOS应用程序,该程序在某些地方有一些音频反馈,但是我希望用户在后台播放的任何其他音乐都可以在此播放。 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. 根据开发人员文档,应通过AVAudioSession环境类别全部启用此功能。 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. 该代码可以很好地执行,并且确实可以使应用程序的声音通过iPod音乐播放。 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. 我尝试将这段代码换成类似的C音频调用(诸如AudioSessionSetProperty之类的东西),而不是Objective-C调用,但是我得到了相同的结果-环境会话类别根本不想尊重静音开关,尽管文档说应该这样做。

Any ideas? 有任何想法吗? Thanks for the help :) 谢谢您的帮助 :)

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

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. 如果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);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM