flutter_tts icon indicating copy to clipboard operation
flutter_tts copied to clipboard

[BUG] setProgressHandler returns reset start/end values after pause and resume

Open alirezabashi98 opened this issue 6 months ago • 2 comments

🐛 Bug Report Summary When using flutter_tts and highlighting spoken words using the setProgressHandler, everything works fine on the first speak() call. However, after pausing the speech and restarting it using speak() again (from the remaining part of the text), the start and end values in setProgressHandler reset to 0, which causes incorrect text highlighting.

Expected behavior After pausing and restarting speech, the start and end values provided by setProgressHandler should be relative to the full text or at least allow compensation so that highlighting remains consistent and continues from where it left off.

Reproduction steps Start speech using flutterTts.speak(story).

Use setProgressHandler to highlight the current word being spoken (using start and end).

Pause the speech (using flutterTts.pause()).

Restart the speech using flutterTts.speak(story.substring(_lastSpokenIndex)).

Observe that start and end values reset to 0, breaking the word highlighting logic.

Code Sample flutterTts.setProgressHandler( (text, start, end, word) { _currentWordStart = (_lastSpokenIndex ?? 0) + start; _currentWordEnd = (_lastSpokenIndex ?? 0) + end; setState(() {}); }, );

flutterTts.setPauseHandler( () { _lastSpokenIndex = _currentWordEnd ?? _lastSpokenIndex; setState(() {}); }, ); // Highlighting text in RichText TextSpan( style: widget.style, children: [ TextSpan(text: widget.story.substring(0, _currentWordStart)), if (_currentWordStart != null) TextSpan( text: widget.story.substring(_currentWordStart!, _currentWordEnd), style: const TextStyle( color: Colors.white, backgroundColor: Colors.amber, ), ), if (_currentWordEnd != null) TextSpan( text: widget.story.substring(_currentWordEnd!), style: const TextStyle(color: Colors.grey), ), ], ); Platform Android 15 (Emulator)

Version flutter_tts: latest (^4.2.3)

dart Copy Edit

alirezabashi98 avatar Jun 03 '25 10:06 alirezabashi98

I have gotten this issue while I test on Samsung Galaxy S10.

aiviapp avatar Jun 07 '25 08:06 aiviapp

I think that's the expected behaviour. On Android, there is no pause option available so the package uses a workaround. It is mentioned in the Android section of this package's readme.

vaibhav891 avatar Jul 03 '25 16:07 vaibhav891