Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import AVFoundation
- let currentRoute = AVAudioSession.sharedInstance().currentRoute
- for description in currentRoute.outputs {
- if description.portType == AVAudioSessionPortLineOut {
- }else if description.portType == AVAudioSessionPortHeadphones {
- }else if description.portType == AVAudioSessionPortBluetoothA2DP{
- }else if description.portType == AVAudioSessionPortBuiltInReceiver{
- }else if description.portType == AVAudioSessionPortBuiltInSpeaker{
- }else if description.portType == AVAudioSessionPortHDMI{
- }else if description.portType == AVAudioSessionPortAirPlay{
- }else if description.portType == AVAudioSessionPortBluetoothLE{
- }
- }
- func IsHeadSetConnected() -> Bool{
- let route = AVAudioSession.sharedInstance().currentRoute;
- for desc in route.outputs
- {
- let portType = desc.portType;
- if (portType == AVAudioSessionPortHeadphones)
- {
- return true;
- }
- }
- return false;
- }
- NotificationCenter.default.addObserver(self, selector: #selector(handleRouteChange(_:)), name: NSNotification.Name.AVAudioSessionRouteChange, object: nil)
- func handleRouteChange(_ notification: Notification) {
- guard
- let userInfo = notification.userInfo,
- let reasonRaw = userInfo[AVAudioSessionRouteChangeReasonKey] as? NSNumber,
- let reason = AVAudioSessionRouteChangeReason(rawValue: reasonRaw.uintValue)
- else { fatalError("Strange... could not get routeChange") }
- switch reason {
- case .oldDeviceUnavailable:
- print("oldDeviceUnavailable")
- case .newDeviceAvailable:
- print("headset/line plugged in")
- case .routeConfigurationChange:
- print("headset pulled out")
- case .categoryChange:
- print("Just category change")
- default:
- print("not handling reason")
- }
- }
- func isHeadphonesConnected() -> Bool{
- let routes = AVAudioSession.sharedInstance().currentRoute
- return routes.outputs.contains(where: { (port) -> Bool in
- port.portType == AVAudioSessionPortHeadphones
- })
- }
- if let availableInputs = AVAudioSession.sharedInstance().availableInputs {
- for route in availableInputs {
- if ( route.portType == AVAudioSessionPortBuiltInMic ) {
- //Headphone available
- break;
- }
- }
- }
- NSString *deviceType = [UIDevice currentDevice].model;
- if([deviceType isEqualToString:@"iPhone"]) {
- //iPhone
- }
- if let availableInputs = AVAudioSession.sharedInstance().availableInputs {
- for route in availableInputs {
- if ( route.portType == AVAudioSessionPortBuiltInMic ) {
- //Headphone available
- break;
- }
- }
- }
Add Comment
Please, Sign In to add comment