Skip to content

Instantly share code, notes, and snippets.

@lucianoschillagi
Last active November 11, 2023 12:50
Show Gist options
  • Save lucianoschillagi/1b4736be808f852746240ec2b67db725 to your computer and use it in GitHub Desktop.
Save lucianoschillagi/1b4736be808f852746240ec2b67db725 to your computer and use it in GitHub Desktop.
Escucha lo que escribiste mediante AVSpeechSynthesizer
import SwiftUI
import AVFoundation
struct SpeechText: View {
@State private var text = ""
var body: some View {
ZStack {
Color.pink.opacity(0.5).ignoresSafeArea()
VStack {
Spacer()
TextField("Mi mensaje", text: $text)
.foregroundColor(.blue)
.font(.largeTitle)
.fontDesign(.rounded)
.bold()
Spacer()
Button("Escuchar lo que escribí") {
speakText()
}.buttonStyle(.borderedProminent)
.buttonBorderShape(.capsule)
.shadow(radius: 10)
}
.padding(.all)
}
}
func speakText() {
let utterance = AVSpeechUtterance(string: text)
utterance.voice = AVSpeechSynthesisVoice(language: "es-MX")
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