我已经从 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: ()
}
}
答案 0 :(得分:1)
解决这个问题最人性化的方法是使用 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) {
}
}
答案 1 :(得分:0)
您应该像以下示例一样配置默认音频会话以使用您想要的内容:
do {
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, mode: .videoChat, options: [.mixWithOthers, .defaultToSpeaker, .allowBluetooth, .allowAirPlay])
} catch {
print(error)
}