How to nest avspeechsynthisizier into sound file (swif3)

I would like to be able to use avspeechsynthiesizer to create a sound file that can be played in other functions. Just like I could by importing a sound into Xcode. So I would like to use the button in my code below to save a sound. Then have it stored function playsound.

  @IBAction func do(_ sender: Any) {
  let utterance = AVSpeechUtterance(string: "Hello")
    utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")

 let synthesizer = AVSpeechSynthesizer()
  synthesizer.delegate = self
  synthesizer.speak(utterance)

 }


  func playSound() {
//like to insert the sound here. 
 let url = Bundle.main.url(forResource: "", withExtension: "")!

do {
player = try AVAudioPlayer(contentsOf: url)
guard let player = player else { return }

player.prepareToPlay()
player.play()
} catch let error as NSError {
print(error.description)
}}

Hi @zalubski

AVSpeachSynthesizer doesn’t have an API for that, i suggest you to take a look at this answer - ios - AVSpeechSynthesizer utterance to audio file - Stack Overflow. It looks quite tricky, but i guess you don’t have much options here.

Nikita

This topic was automatically closed after 166 days. New replies are no longer allowed.