1

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

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • What's the used language? are you sure that you were connect to the internet (https://stackoverflow.com/questions/42900254/swift-3-is-there-a-way-to-use-ios-speech-recognition-in-offline-mode/42900928#42900928)? – Ahmad F Aug 24 '17 at 13:21
  • yes, my internet connected. –  Aug 24 '17 at 13:22
  • @AhmadF i create demo Text-to-speech and Speech-to-Text. both functionality. –  Aug 24 '17 at 13:24

1 Answers1

4

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)