繁体   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