Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift text to speech

I have a block of code that is not working, but not giving me a runtime error either. There is just no speech coming out of the speaker.

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

Is there any code I'm missing out on or is it something else? Help would be much appreciated.

Edit: It's not working in any @IBActions, but is working fine in the view did load function....

override func viewDidLoad() {
    super.viewDidLoad()
    speechRecognizer?.delegate = self
    timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(tick), userInfo: nil, repeats: true)
    tick()
    requestSpeechAuth()
//WORKS HERE
}

@IBAction func audioButtonPressed(_ sender: Any) {
//DOESN"T WORK HERE    
    if isRecording {
        stopRecording()
    } else {
        startRecording()
    }
}
like image 864
Saransh Malik Avatar asked Feb 03 '17 11:02

Saransh Malik


People also ask

How do I convert speech to text in IOS Swift?

Integrate speech recognition swift , create a new state object named speechRecognizer . The initializer requests access to the speech recognizer and microphone the first time the system calls the object. You'll begin transcribing when the meeting view appears and stop recording when the meeting view disappears.

How do I change my Avspeechsynthesizer voice?

No you can't change voice AVSpeechSynthesisVoice(language: "en-US") here. Because it is predefine BCP-47 Code used by apple and can't be manipulated.

How do I use Apple text to speech?

On your Mac, choose Apple menu > System Preferences, click Accessibility , then click Spoken Content. Select the “Speak selection” checkbox. By default, your Mac speaks text when you press the keyboard shortcut Option-Esc.

How do I get text to speech on my Iphone?

Go to Settings > Accessibility > Spoken Content. Adjust any of the following: Speak Selection: To hear text you selected, tap the Speak button. Speak Screen: To hear the entire screen, swipe down with two fingers from the top of the screen.


1 Answers

This code is working (from Apple docs)

let string = "Hello, World!"
let utterance = AVSpeechUtterance(string: string)
utterance.voice = AVSpeechSynthesisVoice(language: "en-US")

let synth = AVSpeechSynthesizer()
synth.speak(utterance)

Remember to import AVFoundation

import AVFoundation
like image 134
Alex Tarragó Avatar answered Oct 27 '22 02:10

Alex Tarragó