AVSpeechSynthesizer not working in iOS 16 Beta

I am using below piece of code for TTS in iOS:

let utterance = AVSpeechUtterance(string: "Hello World")
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")
let synthesizer = AVSpeechSynthesizer()
synthesizer.speak(utterance)

It is working fine for iOS 15.6.1 and all lower versions. But the same code is giving below exception in iOS 16 Beta (latest beta 6 as well):

[catalog] Unable to list voice folder

The required voices are present in the iPhone and working properly in Voice Over and Spoken content. Even the voice API AVSpeechSynthesisVoice.speechVoices() is fetching all the voices, but I am getting above exception at line synthesizer.speak(utterance).

Answered by Engineer in 725057022

Hey there! I notice that you are declaring the AVSpeechSynthesizer as a local variable above. As soon as an AVSpeechSynthesizer goes out of scope and is deallocated, speech output is stopped. Can you try instead storing the synthesizer as an instance or state variable?

Accepted Answer

Hey there! I notice that you are declaring the AVSpeechSynthesizer as a local variable above. As soon as an AVSpeechSynthesizer goes out of scope and is deallocated, speech output is stopped. Can you try instead storing the synthesizer as an instance or state variable?

I was able to resolve the same issue. Thank you very much!

I wrote the following codes.

struct LessonView: View {
    // "let synthesizer = AVSpeechSynthesizer()" should be written here. If you write A in the
    // text2speech function,
    // synthesizer.speak(utterance) is not worked.
    let synthesizer = AVSpeechSynthesizer()

    func text2speech() {
        // If you write A in the text2speech function, synthesizer.speak(utterance) is not worked.
        // let synthesizer = AVSpeechSynthesizer()
        let utterance = AVSpeechUtterance(string: "This is a pen.")
        utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
        utterance.rate = 0.52
        self.synthesizer.speak(utterance)
    }

    var body: some View {
        Button(action : {
            text2speech()
        }){
            LessonButtonView(iconName: "play.circle.fill", labelText: "Play")
        } 
    }
}

However, the issue happens on simulator with iOS 16. But on physical devices, it works as expected.

I have the exact opposite issue. Calls to AVSpeechSynthesizer fail on an iPhone 12 Pro Max running 16.2, but work as expected on the simulator. I've stripped down projects to the bare minimum for both Storyboard-based and SwiftUI, with the same result, so the scope doesn't seem to be the issue, but I've moved the declaration of the AVSpeechSynthesizer to the enclosing view, just to be sure.

Someone else suggested that the Speech synthesizer was going out of scope. So I added :

 @State var synthesizer = AVSpeechSynthesizer()

Now it works fine in 16.2.

I just use synthesizer.speak(utteramce) as before.

In addition to storing the synthesizer as an instance or state variable, the following helped me to get rid of the error message [catalog] Unable to list voice folder:

struct ContentView: View {
  
  init() {
    AVSpeechSynthesisVoice.speechVoices() // <--  fetch voice dependencies
  }
...
AVSpeechSynthesizer not working in iOS 16 Beta
 
 
Q