AVSpeechSynthesizer not working in iOS 16

Hello there, I have a piece of code that write text-to-speech audios into local files. However it stopped working since iOS 16.

I have checked a similar post and can confirm there is a strong reference to the instance of AVSpeechSynthesizer across the app lifecycle.

This looks like a regression in AVFoundation for iOS 16. Could someone take a look and help?

let utterance = AVSpeechUtterance(string: text.text)
utterance.voice = AVSpeechSynthesisVoice(language: text.languageCode)
let soundDir = try FileManager.default.soundDir()
let fileURL = text.fileURL(baseURL: soundDir)
var avFile: AVAudioFile?

self.avSpeechSynthesizer.write(utterance) { (buffer: AVAudioBuffer) in
  //This closure is never called in iOS 16
  guard let pcmBuffer = buffer as? AVAudioPCMBuffer else {
    fatalError("unknown buffer type: \(buffer)")
  }

   if pcmBuffer.frameLength != 0 {
       if avFile == nil {
            avFile = try? AVAudioFile(
                  forWriting: fileURL,
                  settings: pcmBuffer.format.settings,
                  commonFormat: .pcmFormatInt16,
                  interleaved: false
                )
        }
        try? avFile?.write(from: pcmBuffer)
    }
}

The Xcode console output that might be relevant:

2022-09-17 14:57:44.732219-0700 CueTimer[38975:1142525] [AXTTSCommon] File did not exist at content path: (null) (null). Attempting to fallback to default voice for language: (null)

2022-09-17 14:57:44.741347-0700 CueTimer[38975:1142480] [catalog] Unable to list voice folder

2022-09-17 14:57:44.757943-0700 CueTimer[38975:1142480] [catalog] Unable to list voice folder

2022-09-17 14:57:44.761232-0700 CueTimer[38975:1142480] [catalog] Unable to list voice folder

2022-09-17 14:57:44.772778-0700 CueTimer[38975:1142480] [catalog] Unable to list voice folder

2022-09-17 14:57:44.799620-0700 CueTimer[38975:1142525] [AXTTSCommon] Error: Unable to speak. No speech service: voice: (null) identifier: (null), language: (null), resource: (null)

I was hoping that Xcode 14.1 beta would solve this issue reported above, but unfortunately still the same errors. I have been getting these errors since Xcode 14.0 beta 3. Text-to-Voice is a critically important feature, but has been missing in Xcode 14 and now 14.1 beta. It works perfectly under iOS 15.6 and Xcode 13.x. Why is Apple not including this feature in Xcode 14?

*** The problem in my case has been resolved *** I was declaring 'let speechSynthesizer = AVSpeechSynthesizer()' locally in my function in SwiftUI. It has been working until Xcode 14.

I moved the 'let' to the top in my struct and made it a struct property. With no other code change, the problem is resolved.

I tried but no luck

Update: Looks like this issue was first reported a while back but is still not fixed by now, please report a bug to Apple and move the discussion under this post: https://developer.apple.com/forums/thread/708114

In IOS 16 simulator, go settings -> Spoken content -> Voices -> Select any language and download. after downloading, test the app. It works for me.

Do you know any way to automate this download process? I'd like to use the voices while testing the app. Thank you.

Downloading voices might not always work in Simulator Version 14.0.1 (986.3) with iOS 16.0.

When you first create a simulator (here I created an iOS 16 simulator on an iPhone 11), going to Settings > Accessibility > Spoken Content doesn't include an option to download voices, even if you enable Speak Selection and click under Voices: all you see is an empty screen under Voices.

BUT if you kill settings and reopen it, then go back to Spoken Content, enable Speak Selection, click Voices you'll be presented with a list of languages and the ability to download voices below that.

(I created an animated GIF movie of the simulator behavior, but the forum won't let me add it here)

AVSpeechSynthesizer not working in iOS 16
 
 
Q