swift – AVSpeechSynthesizer is not working on iOS 17 Xcode 15.1


I am trying to use text-to-speech feature on my app and suddenly I think after updating to iOS 17.2 this function is disabled instead, and I am getting these errors while trying to use speakWord function.

#FactoryInstall Unable to query results, error: 5
Unable to list voice folder
Unable to list voice folder
Unable to list voice folder
Unable to list voice folder
Unable to list voice folder
tcp_output [C1.1.1.1:3] flags=[R.] seq=3918325251, ack=3203309855, win=2047 state=CLOSED rcv_nxt=3203309855, snd_una=3918325174

Here is the full class:

class TextToSpeech {

    static let shared = TextToSpeech()
    private let speechSynthesizer = AVSpeechSynthesizer()

    func speakWord(matching text: String, from words: [String]) {
        // Concatenate the array of words into a single text for better language detection
        let combinedText = words.joined(separator: " ")

        // Detect the language of the combined text
        let languageCode = detectLanguage(for: combinedText)

        for word in words {
            if word.lowercased() == text.lowercased() {
                // Speak the matching word
                speak(word, withLanguageCode: languageCode)
                break
            }
        }
    }

    private func speak(_ text: String, withLanguageCode languageCode: String) {
        // Configure the voice based on the detected language
        let voice = AVSpeechSynthesisVoice(language: languageCode)

        // Create an AVSpeechUtterance instance
        let utterance = AVSpeechUtterance(string: text)
        utterance.voice = voice

        // Use DispatchQueue to ensure speech synthesis doesn't block the main thread
        DispatchQueue.main.async {
            self.speechSynthesizer.speak(utterance)
        }
    }

    private func detectLanguage(for text: String) -> String {
        let recognizer = NLLanguageRecognizer()
        recognizer.processString(text)
        guard let languageCode = recognizer.dominantLanguage?.rawValue else {
            return "en-US" // Default to English if detection fails
        }
        return languageCode
    }
}

is there any possible solution to fix this bug?

Latest articles

spot_imgspot_img

Related articles

Leave a reply

Please enter your comment!
Please enter your name here

spot_imgspot_img