flutter_tts
flutter_tts copied to clipboard
Flutter Web : speak does not works in init state
🐛 Bug Report
Expected behavior
Expected to work on page load
Reproduction steps
void initState() {
super.initState();
flutterTts = FlutterTts();
sessionData = SessionData(visitor: VisitorModel(), log: VisitorLogModel());
WidgetsBinding.instance.addPostFrameCallback((_) {
_initSpeech();
_initializeTts();
});
}
Future<void> _initializeTts() async {
try {
log('setting tts language : ${ttsLanguageMap[language]!}');
await flutterTts.setLanguage(ttsLanguageMap[language]!);
await flutterTts.setSpeechRate(1.2).then(
(value) {
setState(() {
isInitialized = true;
});
// _speak(AppLocalizations.of(context)!.promptPhone);
_speak('hello');
},
);
} catch (e) {
print("Error initializing TTS: $e");
}
}
Future<void> _speak(String text) async {
if (mounted && isInitialized) {
try {
await flutterTts.speak(text);
} catch (e) {
print("Error speaking: $e");
}
}
}
Configuration
Version: flutter_tts: ^4.0.2
Platform:
- [ ] web : chrome
The chrome doesnt allow to use the speech without the user permission (by security means). And the permission is only given after the user interact with the website, before that (on initialize for example) it wont work.