Ios 在WebRTC视频通话期间,耳机无法提供音频
我已经从iPhone设备连接到耳机,但在视频通话中听不到耳机发出的声音。它每次都在用扬声器听 请在下面找到响应路线更改的音频代码:Ios 在WebRTC视频通话期间,耳机无法提供音频,ios,swift,webrtc,Ios,Swift,Webrtc,我已经从iPhone设备连接到耳机,但在视频通话中听不到耳机发出的声音。它每次都在用扬声器听 请在下面找到响应路线更改的音频代码: func activateHeadPhonesStatus(){ NotificationCenter.default.addObserver(self, selector: #selector(audioRouteChangeListener(_:)), name: NSNotification.Name.AVAudioSessionRouteChange
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: ()
}
}
您应该配置默认音频会话以使用所需的内容,如以下示例所示:
do {
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .videoChat, options: [.mixWithOthers, .defaultToSpeaker, .allowBluetooth, .allowAirPlay])
} catch {
print(error)
}
解决此问题最方便用户的方法是使用AVRoutePickerView,它允许用户选择适当的输出。您只需使用此包装器并将其作为工具栏按钮之一
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) {
}
}
设置音频类别试试?AVAudioSession.sharedInstance().setCategory(.playback)