0

我正在使用下面的代碼在我的應用程序中說一個字符串。Swift清理AVSpeechSynthesizer說話之前

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嗎?

謝謝

回答

0

我得到了這個在操場上工作。沒有重複。

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