Created
January 15, 2023 19:57
-
-
Save wizard1066/a1258d4fac3f03496beaf126e359d007 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import AVFoundation | |
import Combine | |
var nextPage = PassthroughSubject<Void,Never>() | |
var reporter = PassthroughSubject<String, Never>() | |
class speaking: NSObject, AVSpeechSynthesizerDelegate { | |
var synth:AVSpeechSynthesizer! | |
var lastWords = "" | |
override init() { | |
super.init() | |
synth = AVSpeechSynthesizer() | |
} | |
func speaker(words:[String]) { | |
lastWords = String(words.last!) | |
for word in words { | |
let utterance = AVSpeechUtterance(string: word) | |
utterance.voice = AVSpeechSynthesisVoice(language: "en-US") | |
utterance.rate = 0.5 | |
utterance.postUtteranceDelay = 0.1 | |
synth.delegate = self | |
synth.speak(utterance) | |
} | |
} | |
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, | |
didFinish utterance: AVSpeechUtterance) { | |
if utterance.speechString == lastWords { | |
nextPage.send() | |
} | |
} | |
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, | |
didStart utterance: AVSpeechUtterance) { | |
reporter.send(utterance.speechString) | |
} | |
func speechSynthesizer(_ synthesizer: AVSpeechSynthesizer, willSpeakRangeOfSpeechString characterRange: NSRange, utterance: AVSpeechUtterance) { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment