Initialization / Warmup
Thank you very much for making this plugin!
When testing it on an older Android device, it can take up to 5 seconds for the first call to .speak() to be playing. I've tried calling different methods to get it initialized, so that it plays directly when we call it for the first time, but without success.
Is there such a "warmup"/initialization method existing, or a workaround to get it ready to play instantly?
You could try the init handler which is only supported on Android.
// Android only
flutterTts.setInitHandler(() {
setState(() {
ttsState = TtsState.initialized;
});
});
@dlutton Thanks for your answer!
It seems to me that this callback is used to get notified when the text-to-speech has been initialized, and not to actually initialize it. Is that right?
I tried adding it anyway to test it since you suggested, but it seems like it's never being triggered...
@louisdeveseleer that's correct, when you instantiate FlutterTts then it'll begin the initialization.
@dlutton From what I can observe, it seems like the system (on Android) does at least some of the initialization steps only when .speak is called the first time, and not when FlutterTts is instantiated.
In my use case, FlutterTts is instantiated when the widget is created (with final textToSpeech = FlutterTts()), and only ~10 seconds later do I call textToSpeech.speak(...) for the first time. And the first time it gets called, it takes several seconds to get the sound output (on an older device). On subsequent speak(...) calls, the sound output comes right away. Does that make sense?
As a workaround, I am now calling textToSpeech.speak('') in the widget initialization process, to force the system to get ready for the next speak(...) calls, and that seems to do the trick. If it could be somehow included in the package initialization, things would be cleaner, but it's a detail.
@louisdeveseleer I'll take a look and try to play around with the initialization, but typically once it's attached to the flutter engine through onAttachedToEngine, it initializes. If you're running hot reload or hot restart on your device, then it's not going to run that method again until you detach and attach again.
initialization has been fixed in 4.0.0 and initHandler has been removed.