【问题标题】:Detecting if headphones are plugged into iPhone检测耳机是否插入 iPhone
【发布时间】:2011-04-04 06:41:09
【问题描述】:

有谁知道您是否可以检测到耳机是否已插入 iPhone,如果没有,请从您的应用程序中禁用声音。

我想我可以管理禁用声音,但我还没有找到任何关于检测部分的内容。

谢谢

【问题讨论】:

标签: iphone audio headphones


【解决方案1】:

使用此代码,您可以检测以下之间的变化:

  • 有线麦克风
  • 耳机
  • 线路输出
  • 演讲者

Detecting when an iOS Device connector was plugged/unplugged

注意:由于 iOS 5 部分“audioRouteChangeListenerCallback(...)”行为已被弃用,但您可以通过以下方式对其进行更新:

// kAudioSession_AudioRouteChangeKey_PreviousRouteDescription -> Previous route
// kAudioSession_AudioRouteChangeKey_CurrentRouteDescription -> Current route

CFDictionaryRef newRouteRef = CFDictionaryGetValue(routeChangeDictionary, kAudioSession_AudioRouteChangeKey_CurrentRouteDescription);
NSDictionary *newRouteDict = (NSDictionary *)newRouteRef;

// RouteDetailedDescription_Outputs -> Output
// RouteDetailedDescription_Outputs -> Input

NSArray * paths = [[newRouteDict objectForKey: @"RouteDetailedDescription_Outputs"] count] ? [newRouteDict objectForKey: @"RouteDetailedDescription_Outputs"] : [newRouteDict objectForKey: @"RouteDetailedDescription_Inputs"];

NSString * newRouteString = [[paths objectAtIndex: 0] objectForKey: @"RouteDetailedDescription_PortType"];

// newRouteString -> MicrophoneWired, Speaker, LineOut, Headphone

问候

【讨论】:

    【解决方案2】:

    http://developer.apple.com/iphone/library/samplecode/SpeakHere/Introduction/Intro.html

    在这个项目中有一个代码-sn-p,如果耳机被拔掉,它会暂停录音。也许你可以用它来达到你的效果。

    祝你好运!

    (编辑)

    您必须研究 SpeakHereController.mm 文件。
    我在awakeFromNib方法中找到了这段代码

    // we do not want to allow recording if input is not available
    error = AudioSessionGetProperty(kAudioSessionProperty_AudioInputAvailable, &size, &inputAvailable);
    if (error) printf("ERROR GETTING INPUT AVAILABILITY! %d\n", error);
    btn_record.enabled = (inputAvailable) ? YES : NO;
    
    // we also need to listen to see if input availability changes
    error = AudioSessionAddPropertyListener(kAudioSessionProperty_AudioInputAvailable, propListener, self);
    if (error) printf("ERROR ADDING AUDIO SESSION PROP LISTENER! %d\n", error);
    

    【讨论】:

    • 谢谢,我去看看! :)
    【解决方案3】:

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

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

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

    【讨论】:

    • 最近的 iOS 5 更新已弃用 kAudioSessionProperty_AudioRoute ,请改用 kAudioSessionProperty_AudioRouteDescription
    • 这段代码对我造成了一个错误——我相信是因为必须首先调用 AudioSessionGetPropertySize 莫名其妙。请参阅下面在 iOS6 上工作的我的答案(或者如果它得到支持,则希望在上面 ;-)
    • AudioSessionGetProperty 现已弃用!
    【解决方案4】:

    为了执行一次性检查以确定耳机是否已插入(而不是在拔出时设置回调),我在 iOS5 及更高版本中发现了以下工作:

    - (BOOL) isAudioJackPlugged
    {
    
    // initialise the audio session - this should only be done once - so move this line to your AppDelegate
    AudioSessionInitialize(NULL, NULL, NULL, NULL);
    UInt32 routeSize;
    
    // oddly, without calling this method caused an error.
    AudioSessionGetPropertySize(kAudioSessionProperty_AudioRouteDescription, &routeSize);
    CFDictionaryRef desc; // this is the dictionary to contain descriptions
    
    // make the call to get the audio description and populate the desc dictionary
    AudioSessionGetProperty (kAudioSessionProperty_AudioRouteDescription, &routeSize, &desc);
    
    // the dictionary contains 2 keys, for input and output. Get output array
    CFArrayRef outputs = CFDictionaryGetValue(desc, kAudioSession_AudioRouteKey_Outputs);
    
    // the output array contains 1 element - a dictionary
    CFDictionaryRef dict = CFArrayGetValueAtIndex(outputs, 0);
    
    // get the output description from the dictionary
    CFStringRef output = CFDictionaryGetValue(dict, kAudioSession_AudioRouteKey_Type);
    
    /**
     These are the possible output types:
     kAudioSessionOutputRoute_LineOut
     kAudioSessionOutputRoute_Headphones
     kAudioSessionOutputRoute_BluetoothHFP
     kAudioSessionOutputRoute_BluetoothA2DP
     kAudioSessionOutputRoute_BuiltInReceiver
     kAudioSessionOutputRoute_BuiltInSpeaker
     kAudioSessionOutputRoute_USBAudio
     kAudioSessionOutputRoute_HDMI
     kAudioSessionOutputRoute_AirPlay
     */
    
    return CFStringCompare(output, kAudioSessionOutputRoute_Headphones, 0) == kCFCompareEqualTo;
    }
    

    对于那些在家里记分的人来说,这是字典中的字典中的字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-04-24
      • 2011-09-09
      • 2016-02-25
      • 2013-04-17
      • 2013-04-29
      • 2012-05-27
      • 1970-01-01
      • 2015-05-09
      相关资源
      最近更新 更多