0

I am using the code below to speak a string in my app.

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)

If the string is then changed and asked to read again, it repeats the previous string at the end of the new one.

Is it possible to clear AVSpeechSynthesizer before beginning?

Thank you

Tom Coomer
  • 6,227
  • 12
  • 45
  • 82

1 Answers1

1

I got this to work in a playground. Nothing is repeated.

//: 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)
Forest Kunecke
  • 2,160
  • 15
  • 32