Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Swift playgrounds ios 10 text to speech command code

I'm using swift playgrounds on iOS iPad to create a text to speech command. Below is the code.

import AVFoundation
let synthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance (string: "Say     
Hello")
utterance.rate = 1
synthesizer.speak(utterance:   
AVSpeechUtterance)

//when I hit "run my code". I get the error message "Attempt to evaluate editor placeholder" I don't know what this error means. Hopefully someone can help. Thank you.

like image 448
a27divine Avatar asked Feb 18 '26 11:02

a27divine


1 Answers

utterance: AVSpeechUtterance is just an editor placeholder that tells you what you should put there:

synthesizer.speak(utterance: AVSpeechUtterance)

You need to call it passing it the utterance object you created:

synthesizer.speak(utterance)

To get it to speak, you need a few more lines. Here is the complete code:

import AVFoundation
import PlaygroundSupport

PlaygroundPage.current.needsIndefiniteExecution = true

let synthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: "Say Hello")

utterance.rate = 0.5

synthesizer.speak(utterance)
like image 140
vacawama Avatar answered Feb 20 '26 00:02

vacawama