Created
November 11, 2023 16:40
-
-
Save lucianoschillagi/aee35f737c2ab1af3b1ea05b1dbf7814 to your computer and use it in GitHub Desktop.
Listen your message before to send it
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.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