swiftavfoundationavspeechsynthesizer

AVSpeechSynthesizer isn't working under ios16 anymore


I wrote a little function for a text2speech function for my app. the code works fine until ios16. now I see the following console logs:

022-09-13 18:04:02.274692+0200 Blindzeln_Prototyp[47358:164517] [asset] Failed to get sandbox extensions
2022-09-13 18:04:02.314956+0200 Blindzeln_Prototyp[47358:164517] [catalog] Query for com.apple.MobileAsset.VoiceServicesVocalizerVoice failed: 2
2022-09-13 18:04:02.315688+0200 Blindzeln_Prototyp[47358:164517] [catalog] Unable to list voice folder
2022-09-13 18:04:02.333665+0200 Blindzeln_Prototyp[47358:164517] [catalog] Query for com.apple.MobileAsset.VoiceServices.GryphonVoice failed: 2
2022-09-13 18:04:02.334239+0200 Blindzeln_Prototyp[47358:164517] [catalog] Unable to list voice folder
2022-09-13 18:04:02.338622+0200 Blindzeln_Prototyp[47358:164517] [catalog] Unable to list voice folder
2022-09-13 18:04:02.355732+0200 Blindzeln_Prototyp[47358:164583] [AXTTSCommon] File did not exist at content path: (null) (null). Attempting to fallback to default voice for language: (null)
2022-09-13 18:04:02.420342+0200 Blindzeln_Prototyp[47358:164583] [AXTTSCommon] Error: Unable to speak. No speech service: voice: (null) identifier: (null), language: (null), resource: (null)

here's the simple code:

import Foundation
import AVFoundation

func sayIt(text2speech: String) {
    let utterance = AVSpeechUtterance(string: String(text2speech))
    utterance.voice = AVSpeechSynthesisVoice(language: "de-DE")
    utterance.rate = 0.5
    
    let synthesizer = AVSpeechSynthesizer()
    synthesizer.speak(utterance)
}

any hints?


Solution

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

    The system doesn’t automatically retain the speech synthesizer so you need to manually retain it until speech concludes.

    Simply move let synthesizer = AVSpeechSynthesizer() in to a place where it can live in the memory at least the speech is finished.