首页 > 解决方案 > 扬声器问题 - WebRTC 和 iOS

问题描述

我正在尝试使用谷歌的 webRTC pod 制作带有 webRTC 的视频聊天应用程序。我面临的问题是尝试使用扬声器进行声音输出。如果访问扬声器成功,声音开始从所有扬声器发出,包括顶部的扬声器和扬声器;并且有很多回声和噪音。但有时,它根本无法访问扬声器,我看到这个错误:

Error Domain=NSOSStatusErrorDomain Code=-50 

我主要从这个 repo 中使用了 webRTC 部分: https ://github.com/lg-tawsenior/WebRTC-iOS/blob/master/WebRTC-Demo-App/Sources/Services/WebRTCClient.swift

尝试通过扬声器输出的功能是:

func speakerOn() {
      self.audioQueue.async { [weak self] in
          guard let self = self else {
              return
          }
        
          self.rtcAudioSession.lockForConfiguration()
          do {
            
            try self.rtcAudioSession.setCategory(AVAudioSession.Category.playAndRecord.rawValue)
            try self.rtcAudioSession.overrideOutputAudioPort(AVAudioSession.PortOverride.speaker)
            try self.rtcAudioSession.setActive(true)
            
          } catch let error {
            
            debugPrint("Couldn't force audio to speaker: \(error)")
          }
          self.rtcAudioSession.unlockForConfiguration()
      }
  }

我尝试将 rtcAudioSession 设置为非活动状态,但这是不可能的,因为正在进行 I/O。我试图暂停 webRTC 收发器中的所有远程轨道,但没有成功。

非常感谢您的帮助。

标签: swiftwebrtcavaudiosession

解决方案


首先我想删除这个问题,然后想是否有人像我一样偶然发现它,所以:

以下解决了有时无法访问的问题。

self.rtcAudioSession.setCategory(AVAudioSession.Category.playAndRecord.rawValue, with: [.defaultToSpeaker, .allowBluetoothA2DP, .allowAirPlay, .allowBluetooth])

至于听筒和扬声器发出的声音,我尝试了多个 VoIP 应用程序,它的行为相同。看起来这就是苹果希望它工作的方式,所以没有什么可以干扰的。


推荐阅读