Autoplay page narration after a three-second delay

This commit is contained in:
Hadrian Burkhardt
2026-11-02 05:35:14 +01:00
parent a3ead03b6b
commit 4887eca6d4
10 changed files with 327 additions and 73 deletions
+27 -1
View File
@@ -16,6 +16,8 @@ class Narrator extends ChangeNotifier {
final List<StreamSubscription<dynamic>> _subscriptions = [];
String page = '', error = '';
bool loading = false;
Timer? _autoplay;
bool get autoplayPending => _autoplay?.isActive ?? false;
int _generation = 0;
bool get playing =>
_player.playing && _player.processingState != ProcessingState.completed;
@@ -36,7 +38,9 @@ class Narrator extends ChangeNotifier {
String get status => error.isNotEmpty
? error
: page.isEmpty
? 'A recorded reading, kept with this page'
? autoplayPending
? 'The reading starts automatically in 3 seconds'
: 'A recorded reading, kept with this page'
: finished
? 'End of the reading · listen again whenever you like'
: '${playing ? 'Playing' : 'Paused'} · ${_time(position)} / ${_time(_player.duration ?? Duration.zero)}';
@@ -61,7 +65,26 @@ class Narrator extends ChangeNotifier {
return tracks[page]?['speaker'] ?? '';
}
void scheduleAutoplay(String? id, double speed) {
cancelAutoplay();
if (id == null || !tracks.containsKey(id)) return;
_autoplay = Timer(const Duration(seconds: 3), () {
_autoplay = null;
notifyListeners();
if (!playing && !loading) unawaited(toggle(id, speed));
});
notifyListeners();
}
void cancelAutoplay() {
if (_autoplay == null) return;
_autoplay!.cancel();
_autoplay = null;
notifyListeners();
}
Future<void> toggle(String id, double speed) async {
cancelAutoplay();
if (loading) return;
if (playing && page == id) {
await _player.pause();
@@ -105,10 +128,12 @@ class Narrator extends ChangeNotifier {
}
Future<void> pause() async {
cancelAutoplay();
await _player.pause();
}
Future<void> stop() async {
cancelAutoplay();
++_generation;
page = '';
loading = false;
@@ -119,6 +144,7 @@ class Narrator extends ChangeNotifier {
@override
void dispose() {
_autoplay?.cancel();
for (final s in _subscriptions) {
unawaited(s.cancel());
}