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()
}
}
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With