ios - 在说话之前快速清除 AVSpeechSynthesizer

标签 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?

谢谢

最佳答案

我让它在 Playground 上工作。没有重复。

//: 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)

关于ios - 在说话之前快速清除 AVSpeechSynthesizer,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43103179/

相关文章:

javascript - jQuery .closest() 方法在 iOS 设备上返回未定义

ios - 如何在我的网站中关闭自动 sleep ios

ios - React-Native 混合应用程序 : Pop UIViewController that embeds RCTRootView

ios - Swift:使用字典将 alamofire 中的 JSON 数据解析为数组时出错

ios - 在 iOS 中访问所有电影帧的最佳方式

ios - 无法播放声音 : application crashes

ios - 将 AVAudioRecorderDelegate 分配给非 UIViewController

ios - cordova-降级iOS版本

iOS swift : How to dismiss keyboard before compute-intensive code?

swift - 将静态方法的引用传递给函数