user8494663
user8494663

Reputation:

Text to speech not working , When getting text using speech to text

For Speech to text - I used "Speech Framework" and create demo using this link. But after getting a text, I want to speak text field text.

var synth = AVSpeechSynthesizer()
var myUtterance = AVSpeechUtterance(string: textfield.text)
myUtterance.rate = 0.3
synth.speak(myUtterance)

but above code not working. here, i attached demo project link : demo project link

Upvotes: 1

Views: 1128

Answers (1)

CaglayanSerbetci
CaglayanSerbetci

Reputation: 116

your code is working but you couldn't hear the voice because when you start the recording you set the audioSession's category as "AVAudioSessionCategoryRecord". This category is for recording audio and silences playback audio. Then you have to change this settings to hear any voice. Try this:

let audioSession = AVAudioSession.sharedInstance()  //2
    do {
        try audioSession.setCategory(AVAudioSessionCategorySoloAmbient)
        try audioSession.setMode(AVAudioSessionModeSpokenAudio)
        try audioSession.setActive(true, with: .notifyOthersOnDeactivation)
    } catch {
        print("audioSession properties weren't set because of an error.")
    }


    myUtterance = AVSpeechUtterance(string: textView.text!)
    myUtterance.rate = 0.3
    synth.speak(myUtterance)

Upvotes: 4

Related Questions