Enhance postcard puzzles and narration
This commit is contained in:
+61
-34
@@ -1,4 +1,6 @@
|
||||
import 'browser_assets.dart';
|
||||
import 'web_narration_stub.dart'
|
||||
if (dart.library.js_interop) 'web_narration_web.dart';
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
@@ -8,6 +10,7 @@ import 'package:audio_session/audio_session.dart';
|
||||
|
||||
class Narrator extends ChangeNotifier {
|
||||
Narrator(this.tracks) {
|
||||
if (kIsWeb) _web = WebNarration(_webChanged);
|
||||
_subscriptions.add(
|
||||
_player.playerStateStream.listen((_) => notifyListeners()),
|
||||
);
|
||||
@@ -21,9 +24,13 @@ class Narrator extends ChangeNotifier {
|
||||
);
|
||||
}
|
||||
(int, String, String)? _positionState;
|
||||
bool _disposed = false;
|
||||
WebNarration? _web;
|
||||
void _webChanged() => notifyListeners();
|
||||
final ValueNotifier<String> highlightedPassage = ValueNotifier('');
|
||||
@override
|
||||
void notifyListeners() {
|
||||
if (_disposed) return;
|
||||
highlightedPassage.value = passage;
|
||||
super.notifyListeners();
|
||||
}
|
||||
@@ -41,11 +48,16 @@ class Narrator extends ChangeNotifier {
|
||||
Timer? _autoplay;
|
||||
bool get autoplayPending => _autoplay?.isActive ?? false;
|
||||
int _generation = 0;
|
||||
bool get playing =>
|
||||
_player.playing && _player.processingState != ProcessingState.completed;
|
||||
bool get finished => _player.processingState == ProcessingState.completed;
|
||||
bool get playing => kIsWeb
|
||||
? _web!.playing
|
||||
: _player.playing && _player.processingState != ProcessingState.completed;
|
||||
bool get finished => kIsWeb
|
||||
? _web!.finished
|
||||
: _player.processingState == ProcessingState.completed;
|
||||
bool get paused => page.isNotEmpty && !playing && !finished && !loading;
|
||||
Duration get position => _player.position;
|
||||
Duration get position => kIsWeb ? _web!.position : _player.position;
|
||||
Duration get duration =>
|
||||
kIsWeb ? _web!.duration : (_player.duration ?? Duration.zero);
|
||||
String get label => error.isNotEmpty
|
||||
? 'Play this reading'
|
||||
: loading
|
||||
@@ -67,7 +79,7 @@ class Narrator extends ChangeNotifier {
|
||||
: '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)}';
|
||||
: '${playing ? 'Playing' : 'Paused'} · ${_time(position)} / ${_time(duration)}';
|
||||
String get passage {
|
||||
if (page.isEmpty || finished) return '';
|
||||
for (final cue in tracks[page]?['cues'] ?? []) {
|
||||
@@ -111,45 +123,56 @@ class Narrator extends ChangeNotifier {
|
||||
cancelAutoplay();
|
||||
if (loading) return;
|
||||
if (playing && page == id) {
|
||||
if (kIsWeb) {
|
||||
_web!.pause();
|
||||
return;
|
||||
}
|
||||
await _player.pause();
|
||||
return;
|
||||
}
|
||||
final generation = ++_generation;
|
||||
error = '';
|
||||
if (kIsWeb) {
|
||||
final reload = page != id;
|
||||
final asset =
|
||||
tracks[id]['asset'] as String? ??
|
||||
'assets/audio/${tracks[id]['file']}';
|
||||
page = id;
|
||||
loading = true;
|
||||
notifyListeners();
|
||||
try {
|
||||
// The JS play() call happens before the first await, so a tap on Play
|
||||
// retains the browser's transient user activation.
|
||||
await _web!.play(
|
||||
browserAudioUrl(asset, version: tracks[id]['wavSha256'] as String?),
|
||||
speed,
|
||||
reload: reload,
|
||||
);
|
||||
if (generation != _generation) return;
|
||||
loading = false;
|
||||
notifyListeners();
|
||||
} catch (_) {
|
||||
if (generation != _generation) return;
|
||||
loading = false;
|
||||
error = 'Tap Play again to allow audio in your browser.';
|
||||
notifyListeners();
|
||||
}
|
||||
return;
|
||||
}
|
||||
try {
|
||||
if (page != id) {
|
||||
loading = true;
|
||||
error = '';
|
||||
notifyListeners();
|
||||
if (!kIsWeb) {
|
||||
final session = await AudioSession.instance;
|
||||
await session.configure(const AudioSessionConfiguration.speech());
|
||||
}
|
||||
final session = await AudioSession.instance;
|
||||
await session.configure(const AudioSessionConfiguration.speech());
|
||||
if (generation != _generation) return;
|
||||
page = id;
|
||||
final asset =
|
||||
tracks[id]['asset'] as String? ??
|
||||
'assets/audio/${tracks[id]['file']}';
|
||||
if (kIsWeb) {
|
||||
final url = browserAudioUrl(asset);
|
||||
if (_player.audioSources.isEmpty) {
|
||||
await _player.setUrl(url, initialPosition: Duration.zero);
|
||||
} else {
|
||||
// The web backend caches the root playlist by ID. Reusing setUrl
|
||||
// on an active player can replay its cached first child. Mutate
|
||||
// the playlist through its API so the browser receives the new
|
||||
// source, while retaining the already-authorized media element.
|
||||
await _player.pause();
|
||||
await _player.clearAudioSources();
|
||||
if (generation != _generation) return;
|
||||
await _player.addAudioSource(AudioSource.uri(Uri.parse(url)));
|
||||
if (generation != _generation) return;
|
||||
await _player.seek(Duration.zero, index: 0);
|
||||
}
|
||||
} else {
|
||||
// PCM WAV remains the tested format for the Android tablet.
|
||||
await _player.setAsset(asset);
|
||||
}
|
||||
// PCM WAV remains the tested format for the Android tablet.
|
||||
await _player.setAsset(asset);
|
||||
if (generation != _generation) return;
|
||||
} else if (finished) {
|
||||
await _player.seek(Duration.zero);
|
||||
@@ -162,7 +185,7 @@ class Narrator extends ChangeNotifier {
|
||||
_player.play().catchError((Object e) {
|
||||
if (generation != _generation) return;
|
||||
unawaited(_player.pause());
|
||||
error = kIsWeb ? 'Tap Play again to allow audio in your browser.' : 'This recording could not be played. You can still read the entry.';
|
||||
error = 'This recording could not be played. You can still read the entry.';
|
||||
notifyListeners();
|
||||
}),
|
||||
);
|
||||
@@ -179,6 +202,10 @@ class Narrator extends ChangeNotifier {
|
||||
|
||||
Future<void> pause() async {
|
||||
cancelAutoplay();
|
||||
if (kIsWeb) {
|
||||
_web!.pause();
|
||||
return;
|
||||
}
|
||||
await _player.pause();
|
||||
}
|
||||
|
||||
@@ -190,10 +217,7 @@ class Narrator extends ChangeNotifier {
|
||||
error = '';
|
||||
notifyListeners();
|
||||
if (kIsWeb) {
|
||||
// Retain the browser media element and its audio permission between pages.
|
||||
// stop() tears down the web decoder, making the next page start cold.
|
||||
await _player.pause();
|
||||
await _player.seek(Duration.zero);
|
||||
_web!.stop();
|
||||
} else {
|
||||
await _player.stop();
|
||||
}
|
||||
@@ -201,7 +225,10 @@ class Narrator extends ChangeNotifier {
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_disposed = true;
|
||||
_generation++;
|
||||
_autoplay?.cancel();
|
||||
_web?.dispose();
|
||||
for (final s in _subscriptions) {
|
||||
unawaited(s.cancel());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user