Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert text to speech for OSX in Swift playground

Tags:

xcode

macos

swift

I'm trying to learn how to do text to speech for OSX (not iOS) in Swift. I have a playground with code:

import Cocoa

let synth = NSSpeechSynthesizer()
synth.startSpeaking( "Hello World" )

Which seems to run, but no sound occurs. In Xcode, there is a little blue triangle in the lower left corner, which I press thinking it might do something, but sadly no:

Picture of Xcode interface with blue triangle in lower left corner

Any ideas how to convert text to speech for OSX in a Swift playground? Thanks in advance!

like image 352
Dribbler Avatar asked Nov 09 '22 05:11

Dribbler


1 Answers

NSSpeechSynthesizer's .startSpeaking needs to execute in a background task, but by default this is not possible in a Playground.

You can enable it by importing PlaygroundSupport and setting asynchronous mode, like this:

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
like image 126
Eric Aya Avatar answered Nov 14 '22 23:11

Eric Aya