在 phone 通话期间播放音频

Play audio during phone call

我在 Swift 中创建了一个应用程序,它使用 Twilio 和 CallKit 拨打 phone 电话。在 phone 通话期间,我想通过 phone 的耳机播放音频,例如 "You have been on this call for 2 minutes..." 或至少一种内置系统音频声音。

它的工作方式类似于导航应用程序在您通话时播报方向的工作方式

我怎样才能做到这一点?

我在这里查看了一些类似的问题,但我无法得到答案或更新的答案。

发现一种方法是使用内置的 AVSpeechSynthesizer。查看下面的代码

public func sayMessage(message: String) {
    do {
        try setCategory(AVAudioSessionCategoryPlayAndRecord)
        try setActive(true)
        let synth = AVSpeechSynthesizer()
        print("Routes:: \(currentRoute.outputs)")
        if let currentChannels = currentRoute.outputs.first?.channels {
            synth.outputChannels = currentChannels
            print("Found channels \(currentChannels)")
        }
        let myUtterance = AVSpeechUtterance(string: message)
        synth.speak(myUtterance)

    } catch {
        print(error)
    }
}