代码之家  ›  专栏  ›  技术社区  ›  Josh Buhler

检测iPhone/iPod touch附件

  •  10
  • Josh Buhler  · 技术社区  · 17 年前

    是否可以检测iPod touch/iPhone是否连接了耳机或其他附件?

    我正在构建一个需要麦克风的应用程序,需要知道“Isometing”是否连接了一个麦克风,可以通过Dock连接,也可以使用耳机端口,例如苹果的内置耳机/麦克风附件。

    5 回复  |  直到 8 年前
        1
  •  10
  •   Josh Buhler    17 年前

    最后找到它-初始化音频会话对象后,-audioSessionInitialize()-可以调用audioSessionGetProperty,并获取kaudioSessionProperty的值audioInputAvailable。

    AudioSessionInitialize(NULL, NULL, NULL, NULL);    
    UInt32 propertySize, micConnected;
        AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &propertySize, &micConnected);
        [self updateMicStatus:micConnected]; // user-created method
    

    根据音频会话服务的文档,应该使用它,而不是使用设备型号(iPhone与iPod touch)来确定音频输入是否可用。还可以设置回调函数,通过audioSessionAddPropertyListener()监视对此属性的更改。

    还不确定此属性是否也适用于通过Dock连接器连接的设备,但它似乎适用于耳机插孔。

        2
  •  4
  •   zoul    15 年前

    或者你可以使用:

    if (![[AVAudioSession sharedInstance] inputIsAvailable]) {
        // your code here for no audio input available
    }
    
        3
  •  4
  •   Prine    13 年前

    IOS 6 inputIsAvailable 贬低 . 将来我们需要使用 inputAvailable :

    BOOL audioHWAvailable = audioSession.inputAvailable;
    
        4
  •  0
  •   Georg Schölly Crazy Developer    16 年前

    要确定设备是否有内置麦克风,您只需通过 [UIDevice currentDevice].model 看看是iPhone还是第二代iPod touch。对于插入坞站接口的第三方麦克风,在当前的2.2.1 SDK中不可能这样做,但可能是在更高版本中:)

        5
  •  0
  •   McDowell rahul gupta    14 年前

    这是解决方案,您可能会喜欢它,或者它对您有帮助。

    在使用下面的方法之前,请写这两行

    UInt32 audioRouteOverride = kAudioSessionOverrideAudioRoute_None;
        AudioSessionSetProperty (kAudioSessionProperty_OverrideAudioRoute,sizeof (audioRouteOverride),&audioRouteOverride);
    
    - (void)isHeadsetPluggedIn {
    
        UInt32 routeSize = sizeof (CFStringRef);
        CFStringRef route;
    
        AudioSessionGetProperty (kAudioSessionProperty_AudioRoute,
                                                  &routeSize,
                                                  &route);
    
        //NSLog(@"Error >>>>>>>>>> :%@", error);
        /* Known values of route:
         * "Headset"
         * "Headphone"
         * "Speaker"
         * "SpeakerAndMicrophone"
         * "HeadphonesAndMicrophone"
         * "HeadsetInOut"
         * "ReceiverAndMicrophone"
         * "Lineout"
         */
    
        NSString* routeStr = (NSString*)route;
    
        NSRange headsetRange = [routeStr rangeOfString : @"Headset"];
        NSRange receiverRange = [routeStr rangeOfString : @"Receiver"];
    
        if(headsetRange.location != NSNotFound) {
            // Don't change the route if the headset is plugged in.
            NSLog(@"headphone is plugged in ");
        } 
        else if (receiverRange.location != NSNotFound) {
            // Change to play on the speaker
            NSLog(@"play on the speaker");
    
        } 
        else {
            NSLog(@"Unknown audio route.");
    
        }
    }