Guest User

Untitled

a guest
Apr 22nd, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.73 KB | None | 0 0
  1. import AVFoundation
  2.  
  3. let currentRoute = AVAudioSession.sharedInstance().currentRoute
  4.  
  5. for description in currentRoute.outputs {
  6. if description.portType == AVAudioSessionPortLineOut {
  7.  
  8. }else if description.portType == AVAudioSessionPortHeadphones {
  9.  
  10. }else if description.portType == AVAudioSessionPortBluetoothA2DP{
  11.  
  12. }else if description.portType == AVAudioSessionPortBuiltInReceiver{
  13.  
  14. }else if description.portType == AVAudioSessionPortBuiltInSpeaker{
  15.  
  16. }else if description.portType == AVAudioSessionPortHDMI{
  17.  
  18. }else if description.portType == AVAudioSessionPortAirPlay{
  19.  
  20. }else if description.portType == AVAudioSessionPortBluetoothLE{
  21.  
  22. }
  23. }
  24.  
  25. func IsHeadSetConnected() -> Bool{
  26. let route = AVAudioSession.sharedInstance().currentRoute;
  27. for desc in route.outputs
  28. {
  29. let portType = desc.portType;
  30. if (portType == AVAudioSessionPortHeadphones)
  31. {
  32. return true;
  33. }
  34.  
  35. }
  36.  
  37. return false;
  38. }
  39.  
  40. NotificationCenter.default.addObserver(self, selector: #selector(handleRouteChange(_:)), name: NSNotification.Name.AVAudioSessionRouteChange, object: nil)
  41.  
  42. func handleRouteChange(_ notification: Notification) {
  43. guard
  44. let userInfo = notification.userInfo,
  45. let reasonRaw = userInfo[AVAudioSessionRouteChangeReasonKey] as? NSNumber,
  46. let reason = AVAudioSessionRouteChangeReason(rawValue: reasonRaw.uintValue)
  47. else { fatalError("Strange... could not get routeChange") }
  48. switch reason {
  49. case .oldDeviceUnavailable:
  50. print("oldDeviceUnavailable")
  51. case .newDeviceAvailable:
  52. print("headset/line plugged in")
  53. case .routeConfigurationChange:
  54. print("headset pulled out")
  55. case .categoryChange:
  56. print("Just category change")
  57. default:
  58. print("not handling reason")
  59. }
  60. }
  61.  
  62. func isHeadphonesConnected() -> Bool{
  63. let routes = AVAudioSession.sharedInstance().currentRoute
  64. return routes.outputs.contains(where: { (port) -> Bool in
  65. port.portType == AVAudioSessionPortHeadphones
  66. })
  67. }
  68.  
  69. if let availableInputs = AVAudioSession.sharedInstance().availableInputs {
  70. for route in availableInputs {
  71. if ( route.portType == AVAudioSessionPortBuiltInMic ) {
  72. //Headphone available
  73. break;
  74. }
  75. }
  76. }
  77.  
  78. NSString *deviceType = [UIDevice currentDevice].model;
  79. if([deviceType isEqualToString:@"iPhone"]) {
  80. //iPhone
  81. }
  82.  
  83. if let availableInputs = AVAudioSession.sharedInstance().availableInputs {
  84. for route in availableInputs {
  85. if ( route.portType == AVAudioSessionPortBuiltInMic ) {
  86. //Headphone available
  87. break;
  88. }
  89. }
  90. }
Add Comment
Please, Sign In to add comment