Enhance postcard puzzles and narration
This commit is contained in:
@@ -9,8 +9,11 @@ String artworkAsset(String asset) =>
|
||||
? '${asset.substring(0, asset.length - 4)}.webp'
|
||||
: asset;
|
||||
|
||||
String browserAudioUrl(String asset) => Uri.base
|
||||
.resolve(
|
||||
'assets/${optimizedBrowserMedia ? asset.replaceFirst(RegExp(r'\.wav$'), '.mp3') : asset}',
|
||||
)
|
||||
.toString();
|
||||
/// Hash-versioned URLs prevent an older cached recording replacing new audio.
|
||||
String browserAudioUrl(String asset, {String? version}) {
|
||||
final url = Uri.base.resolve(
|
||||
'assets/${optimizedBrowserMedia ? asset.replaceFirst(RegExp(r'\.wav$'), '.mp3') : asset}',
|
||||
);
|
||||
if (version == null || version.isEmpty) return url.toString();
|
||||
return url.replace(queryParameters: {'v': version}).toString();
|
||||
}
|
||||
|
||||
+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());
|
||||
}
|
||||
|
||||
@@ -89,7 +89,9 @@ class PlayMetrics {
|
||||
.where(
|
||||
(card) => List.generate(
|
||||
3,
|
||||
(slot) => store.flag('done_${card.puzzleKey(slot)}'),
|
||||
(slot) =>
|
||||
store.flag('done_${card.puzzleKey(slot)}') ||
|
||||
store.flag('done_${card.legacyPuzzleKey(slot)}'),
|
||||
).every((done) => done),
|
||||
)
|
||||
.length,
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
class WebNarration {
|
||||
WebNarration(void Function() onChange);
|
||||
bool get playing => false;
|
||||
bool get finished => false;
|
||||
Duration get position => Duration.zero;
|
||||
Duration get duration => Duration.zero;
|
||||
Future<void> play(String url, double speed, {required bool reload}) async {}
|
||||
void pause() {}
|
||||
void stop() {}
|
||||
void dispose() {}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import 'dart:js_interop';
|
||||
|
||||
@JS('lumenNarration.create')
|
||||
external JSNumber _create(JSFunction onChange);
|
||||
@JS('lumenNarration.play')
|
||||
external JSPromise<JSAny?> _play(
|
||||
JSNumber id,
|
||||
JSString url,
|
||||
JSNumber speed,
|
||||
JSBoolean reload,
|
||||
);
|
||||
@JS('lumenNarration.pause')
|
||||
external void _pause(JSNumber id);
|
||||
@JS('lumenNarration.stop')
|
||||
external void _stop(JSNumber id);
|
||||
@JS('lumenNarration.dispose')
|
||||
external void _dispose(JSNumber id);
|
||||
@JS('lumenNarration.playing')
|
||||
external JSBoolean _playing(JSNumber id);
|
||||
@JS('lumenNarration.finished')
|
||||
external JSBoolean _finished(JSNumber id);
|
||||
@JS('lumenNarration.position')
|
||||
external JSNumber _position(JSNumber id);
|
||||
@JS('lumenNarration.duration')
|
||||
external JSNumber _duration(JSNumber id);
|
||||
|
||||
class WebNarration {
|
||||
WebNarration(void Function() onChange) : _callback = onChange.toJS {
|
||||
_id = _create(_callback);
|
||||
}
|
||||
final JSFunction _callback;
|
||||
late final JSNumber _id;
|
||||
bool get playing => _playing(_id).toDart;
|
||||
bool get finished => _finished(_id).toDart;
|
||||
Duration get position =>
|
||||
Duration(milliseconds: _position(_id).toDartDouble.round());
|
||||
Duration get duration =>
|
||||
Duration(milliseconds: _duration(_id).toDartDouble.round());
|
||||
Future<void> play(String url, double speed, {required bool reload}) async {
|
||||
await _play(_id, url.toJS, speed.toJS, reload.toJS).toDart;
|
||||
}
|
||||
|
||||
void pause() => _pause(_id);
|
||||
void stop() => _stop(_id);
|
||||
void dispose() => _dispose(_id);
|
||||
}
|
||||
Reference in New Issue
Block a user