2015-10-26 6 views

ответ

3

Попробуйте это:

let route = AVAudioSession.sharedInstance().currentRoute 

for port in route.outputs { 
    if port.portType == AVAudioSessionPortHeadphones { 
     // Headphones located 
    } 
} 

EDIT: сообщение OP изменение вопрос -

Когда приложение работает вам нужно зарегистрироваться AVAudioSessionRouteChangeNotification, чтобы прослушать изменения следующим образом:

NSNotificationCenter.defaultCenter().addObserver(self, selector:"audioRouteChangeListener:", name: AVAudioSessionRouteChangeNotification, object: nil) 

dynamic private func audioRouteChangeListener(notification:NSNotification) { 
    let audioRouteChangeReason = notification.userInfo![AVAudioSessionRouteChangeReasonKey] as UInt 

    switch audioRouteChangeReason { 
    case AVAudioSessionRouteChangeReason.NewDeviceAvailable.rawValue: 
     println("headphone plugged in") 
    case AVAudioSessionRouteChangeReason.OldDeviceUnavailable.rawValue: 
     println("headphone pulled out") 
    default: 
     break 
    } 
} 
+0

, как я могу определить, если микрофон подключен к устройству, если приложение работает ?. Я хочу, чтобы каждый раз, когда я включаю микрофон, я хочу получать мгновенное уведомление. –

+0

См. Мой обновленный комментарий @ GökhanT! – Abhinav

+0

Спасибо @Abhinav –

0

С быстрым 2.0 этот код работает

func audioRouteChangeListenerCallback (notif: NSNotification){ 
    let userInfo:[NSObject:AnyObject] = notif.userInfo! 
    let routChangeReason = UInt((userInfo[AVAudioSessionRouteChangeReasonKey]?.integerValue)!) 
    switch routChangeReason { 
    case AVAudioSessionRouteChangeReason.NewDeviceAvailable.rawValue: 
     print("Connected"); 
     break; 

    case AVAudioSessionRouteChangeReason.OldDeviceUnavailable.rawValue: 
     do { 
      try AVAudioSession.sharedInstance().overrideOutputAudioPort(AVAudioSessionPortOverride.Speaker) 
     } catch _ { 
     } 
     print("Connected"); 
     break; 

    case AVAudioSessionRouteChangeReason.CategoryChange.rawValue: 
     break; 
    default: 
     break; 
    } 
} 
0

С AVAudioSession вы можете перечислить availableInputs

let session = AVAudioSession.sharedInstance() 

    _ = try? session.setCategory(AVAudioSessionCategoryRecord, withOptions: []) 

    print(AVAudioSession.sharedInstance().availableInputs) 

Он возвращает массив AVAudioSessionPortDescription. И вы можете иметь тип микрофона типа «проводной или встроенный» типа portType.

PS: Он работает только на реальном устройстве, а не на симуляторе.

1

swift3:

NotificationCenter.default.addObserver(self, selector: #selector(audioRouteChangeListener(notification:)), name: NSNotification.Name.AVAudioSessionRouteChange, object: nil) 

@objc private func audioRouteChangeListener(notification: Notification) { 
    let rawReason = notification.userInfo![AVAudioSessionRouteChangeReasonKey] as! UInt 
    let reason = AVAudioSessionRouteChangeReason(rawValue: rawReason)! 

    switch reason { 
    case .newDeviceAvailable: 
     print("headphone plugged in") 
    case .oldDeviceUnavailable: 
     print("headphone pulled out") 
    default: 
     break 
    } 
} 
Смежные вопросы