Last active
November 11, 2023 12:50
-
-
Save lucianoschillagi/1b4736be808f852746240ec2b67db725 to your computer and use it in GitHub Desktop.
Escucha lo que escribiste mediante AVSpeechSynthesizer
This file contains hidden or 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 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