Skip to content

Instantly share code, notes, and snippets.

@lucianoschillagi
Created November 11, 2023 16:40
Show Gist options
  • Save lucianoschillagi/aee35f737c2ab1af3b1ea05b1dbf7814 to your computer and use it in GitHub Desktop.
Save lucianoschillagi/aee35f737c2ab1af3b1ea05b1dbf7814 to your computer and use it in GitHub Desktop.
Listen your message before to send it
import SwiftUI
import AVFoundation
struct SpeechText: View {
@State private var text = ""
var body: some View {
ZStack {
Color.teal.opacity(0.9).ignoresSafeArea()
VStack {
Spacer()
TextField("Write your message", text: $text)
.foregroundColor(.black)
.font(.largeTitle)
.fontDesign(.rounded)
.bold()
Spacer()
Button("Listen to what you wrote") {
speakText()
}
.buttonStyle(.borderedProminent)
.buttonBorderShape(.capsule)
.shadow(radius: 10)
}
.padding(.all)
}
}
func speakText() {
let utterance = AVSpeechUtterance(string: text)
utterance.voice = AVSpeechSynthesisVoice(language: "en-GB")
utterance.rate = 0.1
let synthesizer = AVSpeechSynthesizer()
synthesizer.speak(utterance)
}
}
#Preview {
SpeechText()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment