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).

Accepted Reply

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?

  • Thanks a lot. The suggested code change has fixed the issue, but it was working fine in previous iOS versions.

  • Thanks! It worked for me to.

  • Thanks a lot. This worked for me too.

Add a Comment

Replies

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?

  • Thanks a lot. The suggested code change has fixed the issue, but it was working fine in previous iOS versions.

  • Thanks! It worked for me to.

  • Thanks a lot. This worked for me too.

Add a Comment

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 am doing this on an iPhone 15 Pro Max physical device and it works with iOS 17.1.1 but doesn't work on any emulator in XCode 15.0.1

Add a Comment

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.

  • Same, Is it universal or just in my physical device.

    On simulator and mac working fine

Add a Comment

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.

  • Thanks! This works fine for 16.2 & MacCatalyst

Add a Comment

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
  }
...