How to find out if an external headset is connecte

2020-02-06 04:16发布

Is it possible to detect that the user has an external headset plugged into the iPhone's 3.5mm connector or the 30-pin connector? I want to output audio only to an external audio device, and keep silent if nothing is connected.

3条回答
Melony?
2楼-- · 2020-02-06 04:43

There is nice article about this in Apple documentation: https://developer.apple.com/documentation/avfoundation/avaudiosession/responding_to_audio_session_route_changes

Only you have to verify if portType == AVAudioSessionPortBluetoothA2DP

func setupNotifications() {
    let notificationCenter = NotificationCenter.default
    notificationCenter.addObserver(self,
                               selector: #selector(handleRouteChange),
                               name: .AVAudioSessionRouteChange,
                               object: nil)
}


@objc func handleRouteChange(notification: Notification) {
    guard let userInfo = notification.userInfo,
        let reasonValue = userInfo[AVAudioSessionRouteChangeReasonKey] as? UInt,
        let reason = AVAudioSessionRouteChangeReason(rawValue:reasonValue) else {
            return
    }
    switch reason {
    case .newDeviceAvailable:
        let session = AVAudioSession.sharedInstance()
        for output in session.currentRoute.outputs where output.portType == AVAudioSessionPortBluetoothA2DP {
            headsetConnected = true
            break
        }
    case .oldDeviceUnavailable:
        if let previousRoute =
            userInfo[AVAudioSessionRouteChangePreviousRouteKey] as? AVAudioSessionRouteDescription {
            for output in previousRoute.outputs where output.portType == AVAudioSessionPortBluetoothA2DP {
                headsetConnected = false
                break
            }
        }
    default: ()
    }
}

func isBluetoothHeadsetConnected() -> Bool {
    var result = false
    let session = AVAudioSession.sharedInstance()
    for output in session.currentRoute.outputs where output.portType == AVAudioSessionPortBluetoothA2DP {
        result = true
    }
    return result
}
查看更多
爷的心禁止访问
3楼-- · 2020-02-06 04:49

Call this method to find out the bluetooth headset is connected or not.

First import this framework #import <AVFoundation/AVFoundation.h>

- (BOOL) isBluetoothHeadsetConnected
    {
        AVAudioSession *session = [AVAudioSession sharedInstance];
        AVAudioSessionRouteDescription *routeDescription = [session currentRoute];

        NSLog(@"Current Routes : %@", routeDescription);

        if (routeDescription)
        {
            NSArray *outputs = [routeDescription outputs];

            if (outputs && [outputs count] > 0)
            {
                AVAudioSessionPortDescription *portDescription = [outputs objectAtIndex:0];
                NSString *portType = [portDescription portType];

                NSLog(@"dataSourceName : %@", portType);

                if (portType && [portType isEqualToString:@"BluetoothA2DPOutput"])
                {
                    return YES;
                }
            }
        }

        return NO;
    }
查看更多
▲ chillily
4楼-- · 2020-02-06 04:50

The answer is very similar to the answer to this question, but you'll want to get the kAudioSessionProperty_AudioRoute property instead.

查看更多
登录 后发表回答