本文介绍了检测耳机(非麦克风)是否已插入iOS设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要根据耳机是否插入来更改音频。我知道kAudioSessionProperty_AudioInputAvailable,它会告诉我是否有麦克风,但我想测试任何耳机,而不仅仅是耳机内置麦克风。这可能吗?

I need to change my audio depending on whether or not headphones are plugged in. I'm aware of kAudioSessionProperty_AudioInputAvailable, which will tell me if there's a microphone, but I'd like to test for any headphones, not just headphones with a built-in microphone. Is this possible?

推荐答案

这是我自己的方法,它是在本网站上找到的一个稍微修改过的版本: a href =http://www.iphonedevsdk.com/forum/iphone-sdk-development/9982-play-record-same-time.html =noreferrer> http://www.iphonedevsdk.com/ forum / iphone-sdk-development / 9982-play-record-same-time.html

Here is a method of my own which is a slightly modified version of one found on this site : http://www.iphonedevsdk.com/forum/iphone-sdk-development/9982-play-record-same-time.html

- (BOOL)isHeadsetPluggedIn {
    UInt32 routeSize = sizeof (CFStringRef);
    CFStringRef route;

    OSStatus error = AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
                                              &routeSize,
                                              &route);

    /* Known values of route:
     * "Headset"
     * "Headphone"
     * "Speaker"
     * "SpeakerAndMicrophone"
     * "HeadphonesAndMicrophone"
     * "HeadsetInOut"
     * "ReceiverAndMicrophone"
     * "Lineout"
     */

    if (!error && (route != NULL)) {

        NSString* routeStr = (NSString*)route;

        NSRange headphoneRange = [routeStr rangeOfString : @"Head"];

        if (headphoneRange.location != NSNotFound) return YES;

    }

    return NO;
}

这篇关于检测耳机(非麦克风)是否已插入iOS设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-16 02:55