簡體   English   中英

WebRTC 視頻通話期間無法通過耳機獲得音頻

[英]Audio not available through headphones during WebRTC Video calling

我已經從 iPhone 設備連接到耳機,但在視頻通話期間聽不到耳機的聲音。 每次都在免提電話上聽到。

請在音頻響應路由更改代碼下方找到:

func activateHeadPhonesStatus(){
    NotificationCenter.default.addObserver(self, selector: #selector(audioRouteChangeListener(_:)), name: NSNotification.Name.AVAudioSessionRouteChange, object: nil)
}

@objc func audioRouteChangeListener(_ notification:Notification) {
    guard let userInfo = notification.userInfo,
        let reasonValue = userInfo[AVAudioSessionRouteChangeReasonKey] as? UInt,
        let reason = AVAudioSession.RouteChangeReason(rawValue:reasonValue) else {
            return
    }
    switch reason {
    case .newDeviceAvailable:
        let session = AVAudioSession.sharedInstance()
        for output in session.currentRoute.outputs where output.portType == AVAudioSessionPortHeadphones {
            print("headphone plugged in")
            AppDelObj.providerDelegate.videoCallRTC?.speakerOff()
            break
        }
    case .oldDeviceUnavailable:
        if let previousRoute =
            userInfo[AVAudioSessionRouteChangePreviousRouteKey] as? AVAudioSessionRouteDescription {
            for output in previousRoute.outputs where output.portType == AVAudioSessionPortHeadphones {
                print("headphone pulled out")
                AppDelObj.providerDelegate.videoCallRTC?.speakerOn()
                break
            }
        }
    case .routeConfigurationChange:
        print("routeConfigurationChange")
    case .categoryChange:
        print("categoryChange")
    default: ()
    }
    
}

解決這個問題最人性化的方法是使用 AVRoutePickerView,它允許用戶選擇合適的 output。 您可以只使用此包裝器並將其作為工具欄按鈕之一。

import SwiftUI
import AVKit

struct AudioRoutePickerView: UIViewRepresentable {
    typealias UIViewType = AVRoutePickerView
    
    func makeUIView(context: Context) -> AVRoutePickerView {
        let view = AVRoutePickerView()
        return view
    }
    
    func updateUIView(_ uiView: AVRoutePickerView, context: Context) {
    }
}

您應該配置默認音頻 session 以使用您想要的,如下例所示:

do {
     try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .videoChat, options: [.mixWithOthers, .defaultToSpeaker, .allowBluetooth, .allowAirPlay])
 } catch {
   print(error)
 }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM