Swift avspeechoutrance:如何获取随机数组并使用特定语音?

Swift avspeechoutrance:如何获取随机数组并使用特定语音?,swift,avspeechsynthesizer,avspeechutterance,Swift,Avspeechsynthesizer,Avspeechutterance,在使用Swift发出报价时,我遇到了重复使用同一命令的问题 这是我现在拥有的 import AVFoundation import PlaygroundSupport PlaygroundPage.current.needsIndefiniteExecution = true let QuoteArray = [ "Quote1", "Quote2", "Quote3", ] let max = QuoteArray.count let synthesizer = AVSpeechSyn

在使用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随机引用随机读取,但我似乎无法让它正常工作

如果我修改最后几行而不是
avspeechoutrance(string…
他们说
outrance
,我将无法运行第二个或第三个引号,因为
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."

我不知道如何选择一个随机数组位置

下面是一个在游乐场空白项目中测试的问题解决方案(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()) }
import-AVFoundation
导入UIKit
let synthesizer=AVSpeechSynthesizer()
让QuoteArray=[“Quote1”,
“引用2”,
“Quote3”]
func newrandomEutrance()->avSpeechEutrance{
let outrance=avspeechoutrance(字符串:QuoteArray[Int.random(in:0。。