如何在Swift Playground中将文本转换为OSX的语音 [英] How to convert text to speech for OSX in Swift playground

查看:77
本文介绍了如何在Swift Playground中将文本转换为OSX的语音的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习如何在Swift中为OSX( iOS)执行文本到语音转换.我有一个带有代码的游乐场:

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" )

似乎正在运行,但是没有声音.在Xcode中,左下角有一个蓝色的小三角形,我按此按钮可能会起作用,但可惜没有:

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:

有什么想法如何在Swift操场上将文本转换为OSX的语音?预先感谢!

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

推荐答案

NSSpeechSynthesizer.startSpeaking需要在后台任务中执行,但是默认情况下在操场上是不可能的.

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

您可以通过导入PlaygroundSupport并设置异步模式来启用它,如下所示:

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

import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true

这篇关于如何在Swift Playground中将文本转换为OSX的语音的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆