讲话前快速清除AVSpeechSynthesizer

时间:2017-03-29 20:08:27

标签: ios swift avfoundation avspeechsynthesizer avspeechutterance

我正在使用下面的代码在我的应用中说出一个字符串。

var mySynthesizer = AVSpeechSynthesizer()
var myUtterance = AVSpeechUtterance(string: "Hello World!")
myUtterance.voice = AVSpeechSynthesisVoice(language: "en-US")
myUtterance.pitchMultiplier = 1.15
myUtterance.rate = 0.5
mySynthesizer.speak(utterance)

如果更改了字符串并要求再次读取,则会在新字符串结束时重复上一个字符串。

开始之前是否可以清除AVSpeechSynthesizer?

谢谢

1 个答案:

答案 0 :(得分:1)

我让它在操场上工作。什么都没有重复。

//: Playground - noun: a place where people can play

import UIKit
import AVFoundation
import PlaygroundSupport

// this is needed otherwise the playground program exits before the speech is synthesized.
PlaygroundPage.current.needsIndefiniteExecution = true


var mySynthesizer = AVSpeechSynthesizer()
var helloUtterance = AVSpeechUtterance(string: "Hello World!")
helloUtterance.voice = AVSpeechSynthesisVoice(language: "en-US")
helloUtterance.pitchMultiplier = 1.25
helloUtterance.rate = 0.5
mySynthesizer.speak(helloUtterance)

let responseUtterance = AVSpeechUtterance(string: "Hey human. It's me, the world")
responseUtterance.pitchMultiplier = 0.75
responseUtterance.rate = 0.45
mySynthesizer.speak(responseUtterance)