4

I have tried integrating in my App TTS, but there is no audio-output if I click the Button. Here is the Button code:

struct VocabDetailView: View {
//...
    var body: some View {
//...
        HStack{
            Button("Play") {
                readOut(text: "test")
            }
        }
    }
}
//The code from readOut is:

func readOut(text: String) {
    let utterance = AVSpeechUtterance(string: text)
    utterance.voice = AVSpeechSynthesisVoice(language: "en-US")

    let synth = AVSpeechSynthesizer()
    synth.speak(utterance)
}

In the Output field of Xcode is each time I click the button:

2022-11-12 09:53:08.349877+0100 Vocab-learner[2394:64531] [plugin] AddInstanceForFactory: No factory registered for id <CFUUID 0x600000354580> F8BB1C28-BAE8-11D6-9C31-00039315CD46
2022-11-12 09:53:19.528945+0100 Vocab-learner[2394:64592] [catalog] Unable to list voice folder
2022-11-12 09:53:19.539617+0100 Vocab-learner[2394:64592] [catalog] Unable to list voice folder
2022-11-12 09:53:19.543384+0100 Vocab-learner[2394:64592] [catalog] Unable to list voice folder
2022-11-12 09:53:19.551385+0100 Vocab-learner[2394:64592] [catalog] Unable to list voice folder
2022-11-12 09:53:19.566548+0100 Vocab-learner[2394:64916] [AXTTSCommon] File file:///System/Library/PrivateFrameworks/TextToSpeechMauiSupport.framework/Resources/TTSResources/en-US/Samantha/Contents/user_rules.txt contained data that was not null terminated
2022-11-12 09:53:19.573183+0100 Vocab-learner[2394:64916] [AXTTSCommon] File file:///tmp/com.apple.mobileassetd/AssetsV2/com_apple_MobileAsset_Trial_Siri_SiriTextToSpeech/purpose_auto/253115073e2b38c109deaac3e52d26ad44a84148.asset/AssetData/vocalizer-user-dict.dat contained data that was not null terminated

What does that mean? What do I need to do?

I changed language but it does not work either.

2
  • 2
    Your AVSpeechSynthesizer is going out of scope and being deallocated. It needs to live at least as long as your utterance takes to be spoken, so assign synth to a member variable. Nov 14, 2022 at 8:28
  • 2
    @RhythmicFistman your suspicions were correct - I've made an answer below crediting you Nov 15, 2022 at 7:14

3 Answers 3

6

Creds to @Rhythmic Fistman's comment, which correctly identified the issue!

The AVSpeechSynthesizer was going out of scope as no more references to it were being held when the readOut function exited, so it was deallocated before the TTS could be played. A solution is to hold a reference to the AVSpeechSynthesizer in a member variable in your view, and converting readOut into a method in your view:

struct VocabDetailView: View {
    let synth = AVSpeechSynthesizer()
    
    private func readOut(text: String) {
        let utterance = AVSpeechUtterance(string: text)
        utterance.voice = AVSpeechSynthesisVoice(language: "en-US")

        synth.speak(utterance)
    }

    var body: some View {
        HStack{
            Button("Play") {
                readOut(text: "test")
            }
        }
    }
}
Note: The log lines included in the OP will still be printed out, but are erroneous and don't affect the TTS.
1

There doesn't appear to be anything wrong with the code, so the problem must be something to do with the configuration of your Xcode, the emulator, or real hardware you are testing on... or some combination of the three, OR maybe a misconfiguration of your project in Xcode.

To help isolate the problem, you could create a new Xcode project which only contains a simple "hello world" tts demo such as:

let tts = AVSpeechSynthesizer()

tts.speak(AVSpeechUtterance(string: "hello"))

If that demo works on the same hardware/emulator you are using for testing already, then something is borked in your vocab learner project and so unless someone knows the exact meaning of the errors you got, and knows how to fix it, I would suggest just creating a new XCode project and cut/paste your code over to the new project.

If the demo does NOT work (and gives the same errors you listed), then try the following until it does work:

  • If you were using a real device, try an emulator
  • If you were using an emulator, try a real device
  • Changing/updating the OS of the emulator
  • Updating Xcode
  • Updating the OS of your Mac
3
  • Funny story: I tried on both a iPhone 14 Pro Max simulator and running it on my M1 Pro MacBook Pro with the designed for iPad target, with the exact lines of code from the answer in a fresh project. To my surprise, both targets threw around the same error lines as what OP experienced. However, it worked as expected in an iOS playground. Nov 15, 2022 at 7:06
  • 2
    Turns out @Rhythmic Fistman's comment was correct: the AVSpeechSynthesizer was getting deallocated before the TTS had time to play, and assigning it to a member variable fixed the problem, even though those log lines were still being logged. Turns out they weren't the issue after all! Nov 15, 2022 at 7:08
  • Oopsy daisy. Hope you didnt cut and paste your whole project for nothing.
    – Nerdy Bunz
    Nov 16, 2022 at 5:49
-1

FYI this is a known issue in iOS.

https://developer.apple.com/forums/thread/713848

@CryptoAlgorithm's answer does provide a workaround to the issue where TTS will still play, albeit while the console complains loudly, in the mean time.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.