首页 > 解决方案 > 如何在使用 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)")
    }
}

如果有人能指出我正确的方向,我将不胜感激。

编辑:我启用了音频的背景模式。

标签: swiftwebrtcvoipplaybackcallkit

解决方案


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


推荐阅读