Detect attached audio devices iOS

后端 未结 3 923
一整个雨季
一整个雨季 2021-01-12 06:36

I\'m trying to figure out how to detect which if any audio devices are connected on iphone/ipad/ipod. I know all about the audio route calls and route change callbacks but t

相关标签:
3条回答
  • 2021-01-12 06:53

    In case of iOS 5 you should use:

    CFStringRef newRoute;
    size = sizeof(CFStringRef);
    XThrowIfError(AudioSessionGetProperty(kAudioSessionProperty_AudioRoute, &size, &newRoute), "couldn't get new audio route");
    if (newRoute)
    {
        CFShow(newRoute);
        if (CFStringCompare(newRoute, CFSTR("HeadsetInOut"), NULL) == kCFCompareEqualTo) // headset plugged in
              {
                colorLevels[0] = .3;                
                colorLevels[5] = .5;
              }
        else if (CFStringCompare(newRoute, CFSTR("SpeakerAndMicrophone"), NULL) == kCFCompareEqualTo)
    }
    
    0 讨论(0)
  • 2021-01-12 07:09

    Unfortunately, as of iOS11, it seems there's no API to reliably get the list of the output devices that are currently attached - as soon as the current route changes, you only see 1 device (currently routed) via AVAudioSession's currentRoute.outputs, even though multiple devices may still be attached.

    However, for the input, and that includes Bluetooth devices with HFP profile, if the proper Audio Session mode is used (AVAudioSessionModeVoiceChat or AVAudioSessionModeVideoChat for example), one can get the list of the available input via AVAudioSession's availableInputs, and those inputs are listed there even when that device is not an active route - this is very useful when a user is doing a manual override via MPVolumeView from Bluetooth to the speaker, for example, and since HFP is a 2-way IO (has both input and output), you can judge whether output HFP Bluetooth is still available by looking at the inputs.

    BOOL isBtInputAvailable = NO;
    NSArray *inputs = [[AVAudioSession sharedInstance] availableInputs];
    for (AVAudioSessionPortDescription* port in inputs) {
        if ([port.portType isEqualToString:AVAudioSessionPortBluetoothHFP]) {
            isBtInputAvailable = YES;
            break;
        }
    }
    
    0 讨论(0)
  • 2021-01-12 07:12

    You can get from AudioSession properties a list of InputSources and OutputDestinations. Check out these Session Properties:

    kAudioSessionProperty_InputSources
    kAudioSessionProperty_OutputDestinations
    

    And to query the details of each, you can use:

    kAudioSessionProperty_InputSource
    kAudioSessionProperty_OutputDestination
    
    0 讨论(0)
提交回复
热议问题