swift - 如何在使用 Callkit 时播放声音,例如铃声?
问题描述
我正在尝试使用 webRTC 和 Callkit 实现 VoIP。通话期间音频工作得非常好,但我想在用户发起通话(拨出电话)时向用户播放声音。
当用户发起呼叫并等待接收者接听时,我想播放等待哔声(长哔声)。我可以在不使用 Callkit 的情况下播放声音,但是当我通知 Callkit 有一个拨出电话时,它会以某种方式取消音频。我的假设是它这样做是因为它在拨打电话时 IOS 会使音频静音。
所以我的问题是,当 Callkit 处于活动状态时,我如何播放 mp3 文件。或者这种等待声音是否以某种方式集成在 Callkit 或 WebRTC 中?
我弄乱了音频会话的不同类别,但到目前为止还没有运气。请参阅下面我当前代码的摘要。
public var audioPlayer: AVAudioPlayer?
private init() {
do {
audioPlayer = try AVAudioPlayer(contentsOf: URL(fileURLWithPath: Bundle.main.path(forResource: "dialring", ofType: "mp3")!))
audioPlayer!.prepareToPlay()
audioPlayer!.numberOfLoops = -1 //loop
} catch {
print(error.localizedDescription)
}
}
func provider(_ provider: CXProvider, perform action: CXStartCallAction) {
configureAudioSession()
audioPlayer?.play()
}
func configureAudioSession() {
print("Configuring audio session")
let session = AVAudioSession.sharedInstance()
do {
try AVAudioSession.sharedInstance().setCategory(.playAndRecord, options: [.mixWithOthers])
try session.setMode(AVAudioSession.Mode.voiceChat)
} catch (let error) {
print("Error while configuring audio session: \(error)")
}
}
如果有人能指出我正确的方向,我将不胜感激。
编辑:我启用了音频的背景模式。
解决方案
Threw around the structure a bit and now it works. I am not exactly sure what changed thought. If people face the same issue. - Make sure you keep a strong reference to the audioplayer. - Make sure the mode is either in .playback or .playAndRecord
推荐阅读
- javascript - 在 cypress 中管理多个站点
- php - 为什么我使用 PHP 的 $_SERVER 变量得到一个随机字符串作为 IP 地址?
- javascript - 如何使用Javascript添加n个值
- php - htacess 重写规则给出 500 内部服务器错误
- azure-active-directory - 向 Microsoft Graph App 添加权限将如何影响现有用户?
- python - 如果太长,是否可以剪切长变量名?
- amazon-web-services - 如何在 AWS Elastic Container Service 中使用现有的 docker-compose.yml 文件?
- fonts - 如何根据 DeJaVu 制作自定义字体?
- flutter - I/flutter (29011): NoSuchMethodError: 在 null 上调用了方法“插入”。SQFLITE 颤动
- c++ - char 和 int 中 **(ptr+1) 值的差异