通知を聞くためにヘッドフォンの[スウィフト]挿入と削除

  1、通知の登録

NotificationCenter.default.addObserver(自己、セレクター:セレクタ(audioRouteChangeListenerCallback(通知:))、名称:AVAudioSession.routeChangeNotification、オブジェクト:AVAudioSession.sharedInstance())

  図2に示すように、受信した通知を処理します

FUNC audioRouteChangeListenerCallback @objc(通知:NSNotification){ 
        ガードさせたUserInfo = notification.userInfo、
            聞かせてreasonValue = のUserInfo [AVAudioSessionRouteChangeReasonKey] AS UINT ,? 
            のlet理由 = AVAudioSession.RouteChangeReason(RawValue:reasonValue){
                 リターン
        } 
        スイッチの理由{ 
        ケース.newDeviceAvailable :
             // あなたが遊んスピーカーオフヘッドセットを挿入したとき
            self.agoraKit .setEnableSpeakerphone(偽)?
        ケース.oldDeviceUnavailable:
             // プレイアウトヘッドホン、ターンスピーカー再生
            self.agoraKit .setEnableSpeakerphone(真の)?
        デフォルト:()
        } 
    }

  

  PS:ヘッドフォンの場合

FUNC hasHeadset() - > ブール{ 
        せaudioSession = AVAudioSession.sharedInstance()
        currentRouteせ = audioSession.currentRouteの

        ための出力におけるcurrentRoute.outputs {
             場合 output.portType == AVAudioSession.Port.headphones {
                 戻り 
            } 
        } 
        戻り 
    }

 

おすすめ

転載: www.cnblogs.com/xjf125/p/12655915.html