首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AVSpeechUtterance:如何获取随机数组并使用特定的语音?

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

Stack Overflow用户
提问于 2019-05-24 19:55:41
回答 1查看 314关注 0票数 2

在使用Swift提供引号时,我遇到了重复相同命令的问题。

这是我现在所拥有的

代码语言:javascript
复制
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,我不能运行第2或第3引号,因为SIGBART错误。

有什么想法吗?

代码语言:javascript
复制
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)

如果我试图把它带回到这些话语,我会得到一个

代码语言:javascript
复制
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."

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-05-25 13:03:41

我不知道如何选择一个随机排列的位置.

下面是在操场空白项目中测试您的问题的解决方案( iOS 12):

代码语言:javascript
复制
    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()) }

..。在综合的声音和速度上胡闹。

有一个详细摘要的WWDC 2018年视频处理语音合成器和一个代码片段的完整解释 (ObjC和Swift),如果上面的例子是不够的。

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

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/56298828

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档