swift - 扬声器问题 - 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 收发器中的所有远程轨道,但没有成功。
非常感谢您的帮助。
解决方案
首先我想删除这个问题,然后想是否有人像我一样偶然发现它,所以:
以下解决了有时无法访问的问题。
self.rtcAudioSession.setCategory(AVAudioSession.Category.playAndRecord.rawValue, with: [.defaultToSpeaker, .allowBluetoothA2DP, .allowAirPlay, .allowBluetooth])
至于听筒和扬声器发出的声音,我尝试了多个 VoIP 应用程序,它的行为相同。看起来这就是苹果希望它工作的方式,所以没有什么可以干扰的。
推荐阅读
- java - Spring忽略云上特定包的设置日志级别
- c# - 集成测试给出编译错误 CS0051
- python - 有没有办法使用 pyserial 设置设备 ID?
- javascript - 为什么在将数据设置为上下文时出现超出最大更新深度的错误(React)
- php - PhpScript 的并行化
- ruby-on-rails - 如何在多重搜索中为字段选择不同的“运算符”
- sql - 如何在 MongoDB 中导出脚本创建数据库?
- javascript - Node.js 在创建新存储库时自动降级
- r - 根据 facet_grid 中的行数在 ggsave 中定义“高度”的函数
- java - Spotbugs 排除特定构造函数的 SEC_SIDE_EFFECT_CONSTRUCTOR