24

我过去曾使用此方法来检测是否启用了静音开关:

- (BOOL)silenced {
    #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);

    if(CFStringGetLength(state) > 0)
        return NO;
    else
        return YES;
}

这不再适用于我的 iPad 或 iPhone 上的 iOS5。这是一个已知的问题?我无法在堆栈溢出或 Apple 开发论坛上找到任何答案。

希望你能帮助...

4

2 回答 2

8

自 iOS 5 以来,我们将无法再检测静音开关状态...

Apple 的答案在已接受的答案中:Detecting the iPhone's Ring / Silent / Mute switch using AVAudioPlayer not working?

于 2011-11-07T12:47:21.457 回答
1

正如 iOS 开发者库中提到的,属性 kAudioSessionProperty_AudioRoute 已被弃用。相反,使用 kAudioSessionProperty_AudioRouteDescription

https://developer.apple.com/library/ios/#documentation/AudioToolbox/Reference/AudioSessionServicesReference/Reference/reference.html

这个问题已经在这里得到解答: Detecting the iPhone's Ring / Silent / Mute switch using AVAudioPlayer not working?

于 2011-10-25T09:15:49.137 回答