Reputation: 143
I have an app with Text To Speech feature when clicking on a button. My code worked fine until it made the app crash. I think this bug must be related to the new release of iOS 9.3.3 but I can't find anything on the net that's why I'm posting here.
Here is the function triggered by a button:
func textToSpeech(){
let textSynthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: self.targetLanguage)
utterance.voice = AVSpeechSynthesisVoice(language: self.languageFlag)
textSynthesizer.speakUtterance(utterance)
}
When I click on the button I get this error on the AVSpeechSynthesizer initialisation :
And in console: (lldb)
Does anyone has an idea of how to fix this ? Thanks, Alex
Upvotes: 1
Views: 1140
Reputation: 416
Try this -:
func text2Speech(text: String,languageCode: String) {
do {
try AVAudioSession.sharedInstance().setCategory(.playback,mode: .default)
} catch let error {
print("\(error.localizedDescription)")
}
let speechUtterance: AVSpeechUtterance = AVSpeechUtterance(string: text)
speechUtterance.rate = AVSpeechUtteranceMaximumSpeechRate / 2.0
speechUtterance.voice = AVSpeechSynthesisVoice(language: languageCode)
speechSynthesizer.speak(speechUtterance)
}
Upvotes: 1