在使用Swift进行报价时,我遇到了重复同一命令的问题。
这就是我现在拥有的
import AVFoundation
import PlaygroundSupport
PlaygroundPage.current.needsIndefiniteExecution = true
let QuoteArray = [
"Quote1",
"Quote2",
"Quote3",
]
let max = QuoteArray.count
let synthesizer = AVSpeechSynthesizer()
let utterance = AVSpeechUtterance(string: QuoteArray[Int.random(in: 0 ... max)])
utterance.rate = 0.5
utterance.voice = AVSpeechSynthesisVoice(language: "en-AU")
synthesizer.speak(AVSpeechUtterance(string: QuoteArray[Int.random(in: 0 ... max)]))
sleep(5)
synthesizer.speak(AVSpeechUtterance(string: QuoteArray[Int.random(in: 0 ... max)]))
sleep(10)
synthesizer.speak(AVSpeechUtterance(string: QuoteArray[Int.random(in: 0 ... max)]))
我不确定如何选择随机数组的位置,以及如何混音合成声音和速度。我正在尝试使其能够随机读取3个随机引号,但我似乎无法使其正常工作。
如果我修改了最后几行,而不是AVSpeechUtterance(string...
,而是说utterance
,则由于SIGBART
错误,我无法运行第二或第三引号。
有什么想法吗?
utterance.rate = 35
utterance.pitchMultiplier = 3
utterance.voice = AVSpeechSynthesisVoice(language: "en-AU")
synth.speak(utterance)
sleep(5)
synth.speak(utterance)
sleep(10)
synth.speak(utterance)
sleep(15)
如果我尝试将其恢复为这些话语,我会得到
error: Execution was interrupted, reason: signal SIGABRT.
The process has been left at the point where it was interrupted, use "thread return -x" to return to the state before expression evaluation."
答案 0 :(得分:1)
我不确定如何同时选择随机数组的位置...
为您解决问题的解决方案(swift 5.0,iOS 12)在游乐场空白项目中进行了测试:
import AVFoundation
import UIKit
let synthesizer = AVSpeechSynthesizer()
let QuoteArray = ["Quote1",
"Quote2",
"Quote3"]
func newRandomUtterance() -> AVSpeechUtterance {
let utterance = AVSpeechUtterance(string: QuoteArray[Int.random(in: 0..<QuoteArray.count)])
utterance.rate = AVSpeechUtteranceDefaultSpeechRate
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
return utterance
}
for _ in 1...3 { synthesizer.speak(newRandomUtterance()) }
...,然后混搭合成声音和速度。
有detailed summary的WWDC 2018视频涉及语音合成器,还有full explanation with code snippets (ObjC和Swift),如果上面的示例不是足够。
您现在可以获取随机数组并使用特定的声音。