feat(tts): Improve text-to-speech service with enhanced error handling and state management

This commit is contained in:
cogwheel0
2025-10-30 21:42:35 +05:30
parent d7f58498f9
commit de0f195aea
5 changed files with 238 additions and 58 deletions

View File

@@ -188,10 +188,19 @@ class TextToSpeechController extends Notifier<TextToSpeechState> {
return;
}
final isPausedActive =
state.activeMessageId == messageId &&
state.status == TtsPlaybackStatus.paused;
if (isPausedActive) {
await resume();
return;
}
final isCurrentlyActive =
state.activeMessageId == messageId &&
state.status != TtsPlaybackStatus.idle &&
state.status != TtsPlaybackStatus.error;
state.status != TtsPlaybackStatus.error &&
state.status != TtsPlaybackStatus.paused;
if (isCurrentlyActive) {
await stop();
@@ -294,6 +303,24 @@ class TextToSpeechController extends Notifier<TextToSpeechState> {
await _service.pause();
}
Future<void> resume() async {
if (!state.initialized || !state.available) {
return;
}
try {
await _service.resume();
} catch (e) {
if (!ref.mounted) {
return;
}
state = state.copyWith(
status: TtsPlaybackStatus.error,
errorMessage: e.toString(),
clearActiveMessageId: true,
);
}
}
Future<void> stop() async {
await _service.stop();
if (!ref.mounted) {