I tried the following code which works fine under Mac OS 11 (iPhone 8 Plus simulator Big Sur on Mac mini M1), but does not generate sound under iOS (iPone 8 plus):
I don't know what the issue is:
- Authorisation problem (plist or something)?
- Version of iOS?
- Libraries?
... or how to debug it...
import SwiftUI
import AVFoundation //Speech library
struct ContentView: View {
@State var text: String = "Starting"
var body: some View {
VStack {
Text(text)
.fontWeight(.bold)
.font(.title)
.padding()
Button(action: speak) {
Text("Click to test TTS")
}
}
}
func speak(){
let utterance = AVSpeechUtterance(string: "Hello world")
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
if utterance.voice != nil {
self.text = "Voice available"
} else {
self.text = "Pb: Voice not available"
}
utterance.rate = 0.5
utterance.volume = 1.0
let synthesizer = AVSpeechSynthesizer()
synthesizer.speak(utterance)
}
}