【发布时间】:2018-05-16 00:43:11
【问题描述】:
我正在试验 Apple 的语音合成,我想看看 Alex 的声音在 iOS 设备上的声音效果如何。
有没有办法让 Alex 的声音可以通过 Swift Playground 访问?
【问题讨论】:
标签: ios swift speech-synthesis avspeechsynthesizer
我正在试验 Apple 的语音合成,我想看看 Alex 的声音在 iOS 设备上的声音效果如何。
有没有办法让 Alex 的声音可以通过 Swift Playground 访问?
【问题讨论】:
标签: ios swift speech-synthesis avspeechsynthesizer
是的,如果您添加这两行,它将起作用。
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
由于语音合成器是异步代码,“needsIndefiniteExecution”允许在 Playground 的顶层代码结束后继续执行。这反过来又为线程和回调提供了执行时间。 (from Apple's documentation of Playground Support)
例子:
import AVFoundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let synth = AVSpeechSynthesizer()
let speech = AVSpeechUtterance(string: "Hello, World!")
speech.voice = AVSpeechSynthesisVoice(language: "en-US")
synth.speak(speech)
【讨论】: