2/6/4
This commit is contained in:
@@ -1,25 +1,38 @@
|
||||
import 'package:flutter_tts/flutter_tts.dart';
|
||||
import 'package:get/get.dart';
|
||||
|
||||
class TextToSpeech {
|
||||
final FlutterTts tts = FlutterTts();
|
||||
class TextToSpeechController extends GetxController {
|
||||
final flutterTts = FlutterTts();
|
||||
|
||||
// Initialize the TTS engine with desired settings
|
||||
Future initTts() async {
|
||||
await tts.setLanguage('en-US'); // Set language
|
||||
await tts.setSpeechRate(0.8); // Adjust speech rate
|
||||
await tts.setVolume(1.0); // Set volume
|
||||
// Initialize TTS in initState
|
||||
@override
|
||||
void onInit() {
|
||||
super.onInit();
|
||||
initTts();
|
||||
}
|
||||
|
||||
// Speak the given text
|
||||
Future speakText(String text) async {
|
||||
await tts.speak(text);
|
||||
// Dispose of TTS when controller is closed
|
||||
@override
|
||||
void onClose() {
|
||||
super.onClose();
|
||||
flutterTts.completionHandler;
|
||||
}
|
||||
|
||||
// Stop speaking
|
||||
Future stopSpeaking() async {
|
||||
await tts.stop();
|
||||
// Function to initialize TTS engine
|
||||
Future<void> initTts() async {
|
||||
await flutterTts.setLanguage('en-US'); // Set language
|
||||
await flutterTts.setSpeechRate(0.5); // Adjust speech rate
|
||||
await flutterTts.setVolume(1.0); // Set volume
|
||||
}
|
||||
|
||||
// Check if TTS is currently speaking
|
||||
// bool isSpeaking() => tts.isSpeaking;
|
||||
// Function to speak the given text
|
||||
Future<void> speakText(String text) async {
|
||||
try {
|
||||
await flutterTts.speak(text);
|
||||
} catch (error) {
|
||||
// Handle error gracefully, e.g., show a message
|
||||
Get.snackbar('Error', 'Failed to speak text: $error');
|
||||
print('Failed to speak text: $error');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user