gpt4 book ai didi

swift - AVSpeechUtterance:如何获取随机数组并使用特定语音?

转载 作者:可可西里 更新时间:2023-11-01 01:49:04 28 4
gpt4 key购买 nike

在使用 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."

enter image description here

最佳答案

I'm not sure how to both pick a random array position...

下面是针对您的问题的解决方案(swift 5.0,iOS 12)在 Playground 空白项目中测试:

    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()) }

... and to fool around with the synthesisvoice and speed.

有一个detailed summary处理语音合成器的 WWDC 2018 视频和 full explanation with code snippets (ObjC 和 Swift) 如果上面的示例还不够,这可以提供帮助。

您现在可以获取随机数组并使用特定语音

关于swift - AVSpeechUtterance:如何获取随机数组并使用特定语音?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56298828/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com