audio - iPod touch无法检测到耳机

标签 audio ipod

我使用下面提到的代码来确定是否有“耳机”连接到iOS设备。

//find out, if any earphones are connected to the device
- (BOOL)isHeadsetPluggedIn {
    UInt32 routeSize = sizeof (CFStringRef);
    CFStringRef route;
    NSLog(@"Inside 'isHeadsetPluggedIn'");

    // Registers the audio route change listener callback function
    AudioSessionAddPropertyListener (kAudioSessionProperty_AudioRouteChange, audioRouteChangeListenerCallback, (__bridge void *)(self));

    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*)CFBridgingRelease(route);
        NSRange headphoneRange = [routeStr rangeOfString : @"Head"];
        NSLog(@"route %@", routeStr);
        if (headphoneRange.location != NSNotFound) {
            return YES;
        }
    } else {
        NSLog(@"Error %d while retrieving audio property", error);
    }
    return NO;
}

上面的代码适用于iPad mini,iPad和iPhone设备。
但是在“iPod touch”设备中,“AudioSessionGetProperty”函数在检索音频属性时返回错误“错误” 560557673”。
因此,它不会检测是否有任何“耳机”连接到“iPod touch”设备。

如何在“iPod touch”设备上找到“耳机”?

最佳答案

我可以使用下面提到的方法(从“SO”找到,但不记住地址)解决此问题。

BOOL isAudioRouteAvailable(CFStringRef routeType)
{
    /*
     As of iOS 5:
     kAudioSessionOutputRoute_LineOut;
     kAudioSessionOutputRoute_Headphones;
     kAudioSessionOutputRoute_BluetoothHFP;
     kAudioSessionOutputRoute_BluetoothA2DP;
     kAudioSessionOutputRoute_BuiltInReceiver;
     kAudioSessionOutputRoute_BuiltInSpeaker;
     kAudioSessionOutputRoute_USBAudio;
     kAudioSessionOutputRoute_HDMI;
     kAudioSessionOutputRoute_AirPlay;
     */

    //Prep
    BOOL foundRoute = NO;
    CFDictionaryRef description = NULL;

    //Session
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        AudioSessionInitialize(NULL, NULL, NULL, NULL);
    });

    //Property
    UInt32 propertySize;
    AudioSessionGetPropertySize(kAudioSessionProperty_AudioRouteDescription, &propertySize);
    OSStatus error = AudioSessionGetProperty(kAudioSessionProperty_AudioRouteDescription, &propertySize, &description);
    if ( !error && description ) {
        CFArrayRef outputs = CFDictionaryGetValue(description, kAudioSession_AudioRouteKey_Outputs);
        CFIndex count = CFArrayGetCount(outputs);
        if ( outputs && count ) {
            for (CFIndex i = 0; i < count; i++) {
                CFDictionaryRef route = CFArrayGetValueAtIndex(outputs, i);
                CFStringRef type = CFDictionaryGetValue(route, kAudioSession_AudioRouteKey_Type);
                NSLog(@"Got audio route %@", type);

                //Audio route type
                if ( CFStringCompare(type, routeType, 0) == kCFCompareEqualTo ) {
                    foundRoute = YES;
                    break;
                }
            }
        }
    } else if ( error ) {
        NSLog(@"Audio route error %ld", error);
    }

    //Cleanup
    if ( description ) {
        CFRelease(description);
    }

    //Done
    return foundRoute;  
}

通过打电话,
isAudioRouteAvailable(kAudioSessionOutputRoute_Headphones)

关于audio - iPod touch无法检测到耳机,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19970700/

相关文章:

iphone - 如何在ipod中使用前置摄像头进行条码扫描

iPhone/iPod - 防止字体大小改变

ios - 是否可以仅从 UDID 获取其他设备信息?

ios - 如何防止我的用户从 iPod 中删除 MP3 文件

python - 如何在Python中获取音频文件的脉冲宽度?

ios - 防止多个声音文件在播放时重叠

delphi - Vista/Win7 Delphi 音频设备信息

operating-system - iPod Nano 6G/7G 运行什么操作系统?

java - 音频频谱效果 iOS/Android

android - 会说话的应用程序就像会说话的汤姆猫一样,录音在所有设备上都不起作用