2

I've created an app in Swift that uses Twilio & CallKit to make outgoing phone calls. During the phone call, I would like to play audio through the phone's ear speaker such as "You have been on this call for 2 minutes..." or at the least an one of the built in system audio sounds.

It would work similarly to how navigations apps work when they announce directions when you are on a call

How can I make this happen?

I've looked at some similar questions on here, but I couldn't get an answer or updated answer.

Phil
  • 1,077
  • 1
  • 11
  • 18

1 Answers1

1

Found out one way to do this is by using the built in AVSpeechSynthesizer. Check out the code below

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)
    }
}
Phil
  • 1,077
  • 1
  • 11
  • 18
  • You could also use `AVAudioPlayer` to play a recorded sound, like here: https://stackoverflow.com/questions/24043904/creating-and-playing-a-sound-in-swift – philnash Jun 15 '17 at 09:48
  • @philnash the answer is incomplete. When/where do you call this method? – Vyachaslav Gerchicov Dec 18 '19 at 11:40
  • @VyachaslavGerchicov I don't know, depends on when you want the audio to be played. It's up to you – philnash Dec 18 '19 at 22:24
  • @philnash you wrote `I've created an app in Swift that uses Twilio & CallKit to make outgoing phone calls. During the phone call, I would like to play audio`. Could you please add info how and when do you call your func `sayMessage(message:)`? – Vyachaslav Gerchicov Dec 19 '19 at 11:28
  • Apologies, but that was from the question asker and answerer, which was @Phil, not me. Check your usernames again – philnash Dec 19 '19 at 11:30