flutter_tts icon indicating copy to clipboard operation
flutter_tts copied to clipboard

Flutter Web : speak does not works in init state

Open IamAKX opened this issue 1 year ago • 1 comments

🐛 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

IamAKX avatar May 26 '24 05:05 IamAKX

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.

wagner-araujo-ramos avatar Oct 31 '24 13:10 wagner-araujo-ramos