Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift TTS, no audio-output

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.

like image 499
someone Avatar asked Oct 21 '25 06:10

someone


1 Answers

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.
like image 98
CryptoAlgorithm Avatar answered Oct 24 '25 11:10

CryptoAlgorithm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!