Add browser playtest and improve library, media and input
Bundle web bootstrap, icons, local font fallbacks and hosting instructions. Add optimized WebP/MP3 packaging, asset checks and a browser narration regression tool. Support browser audio handoff and ad-free explained hints. Refresh the responsive bookshelf and reduce unnecessary app, narration and paper repaint work. Preserve deliberate page swipes while tolerating small mouse movements during cell and navigation clicks. Validation: all 77 Flutter tests pass; shell, JavaScript and Python scripts parse; locale and web manifests are valid JSON; git diff --check passes.
This commit is contained in:
@@ -1,3 +1,5 @@
|
||||
import 'browser_assets.dart';
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:audio_session/audio_session.dart';
|
||||
@@ -29,13 +31,19 @@ class _AudioOutput implements AmbienceOutput {
|
||||
|
||||
@override
|
||||
Future<void> load(Soundscape scene) async {
|
||||
final session = await AudioSession.instance;
|
||||
await session.configure(const AudioSessionConfiguration.speech());
|
||||
if (!await session.setActive(true)) {
|
||||
throw StateError('Audio focus unavailable');
|
||||
if (!kIsWeb) {
|
||||
final session = await AudioSession.instance;
|
||||
await session.configure(const AudioSessionConfiguration.speech());
|
||||
if (!await session.setActive(true)) {
|
||||
throw StateError('Audio focus unavailable');
|
||||
}
|
||||
}
|
||||
await _player.setLoopMode(LoopMode.one);
|
||||
await _player.setAsset(scene.asset);
|
||||
if (kIsWeb) {
|
||||
await _player.setUrl(browserAudioUrl(scene.asset));
|
||||
} else {
|
||||
await _player.setAsset(scene.asset);
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
|
||||
@@ -0,0 +1,16 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
|
||||
/// The web packager emits lighter copies; native builds retain original media.
|
||||
const optimizedBrowserMedia =
|
||||
kIsWeb && bool.fromEnvironment('WEB_OPTIMIZED_MEDIA');
|
||||
|
||||
String artworkAsset(String asset) =>
|
||||
optimizedBrowserMedia && asset.endsWith('.png')
|
||||
? '${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();
|
||||
+61
-11
@@ -1,3 +1,5 @@
|
||||
import 'browser_assets.dart';
|
||||
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/foundation.dart';
|
||||
@@ -9,8 +11,23 @@ class Narrator extends ChangeNotifier {
|
||||
_subscriptions.add(
|
||||
_player.playerStateStream.listen((_) => notifyListeners()),
|
||||
);
|
||||
_subscriptions.add(_player.positionStream.listen((_) => notifyListeners()));
|
||||
_subscriptions.add(
|
||||
_player.positionStream.listen((position) {
|
||||
final state = (position.inSeconds, passage, speaker);
|
||||
if (state == _positionState) return;
|
||||
_positionState = state;
|
||||
notifyListeners();
|
||||
}),
|
||||
);
|
||||
}
|
||||
(int, String, String)? _positionState;
|
||||
final ValueNotifier<String> highlightedPassage = ValueNotifier('');
|
||||
@override
|
||||
void notifyListeners() {
|
||||
highlightedPassage.value = passage;
|
||||
super.notifyListeners();
|
||||
}
|
||||
|
||||
Map<String, dynamic> tracks;
|
||||
void replaceTracks(Map<String, dynamic> value) {
|
||||
unawaited(stop());
|
||||
@@ -29,7 +46,9 @@ class Narrator extends ChangeNotifier {
|
||||
bool get finished => _player.processingState == ProcessingState.completed;
|
||||
bool get paused => page.isNotEmpty && !playing && !finished && !loading;
|
||||
Duration get position => _player.position;
|
||||
String get label => loading
|
||||
String get label => error.isNotEmpty
|
||||
? 'Play this reading'
|
||||
: loading
|
||||
? 'Opening recording…'
|
||||
: playing
|
||||
? 'Pause recording'
|
||||
@@ -96,20 +115,41 @@ class Narrator extends ChangeNotifier {
|
||||
return;
|
||||
}
|
||||
final generation = ++_generation;
|
||||
error = '';
|
||||
try {
|
||||
if (page != id) {
|
||||
loading = true;
|
||||
error = '';
|
||||
notifyListeners();
|
||||
final session = await AudioSession.instance;
|
||||
await session.configure(const AudioSessionConfiguration.speech());
|
||||
if (!kIsWeb) {
|
||||
final session = await AudioSession.instance;
|
||||
await session.configure(const AudioSessionConfiguration.speech());
|
||||
}
|
||||
if (generation != _generation) return;
|
||||
page = id;
|
||||
// PCM WAV avoids silent FLAC playback on the API 26 Huawei tablet.
|
||||
await _player.setAsset(
|
||||
tracks[id]['asset'] as String? ??
|
||||
'assets/audio/${tracks[id]['file']}',
|
||||
);
|
||||
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);
|
||||
}
|
||||
if (generation != _generation) return;
|
||||
} else if (finished) {
|
||||
await _player.seek(Duration.zero);
|
||||
@@ -120,7 +160,9 @@ class Narrator extends ChangeNotifier {
|
||||
notifyListeners();
|
||||
unawaited(
|
||||
_player.play().catchError((Object e) {
|
||||
error = 'This recording could not be played. You can still read the entry.';
|
||||
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.';
|
||||
notifyListeners();
|
||||
}),
|
||||
);
|
||||
@@ -147,7 +189,14 @@ class Narrator extends ChangeNotifier {
|
||||
loading = false;
|
||||
error = '';
|
||||
notifyListeners();
|
||||
await _player.stop();
|
||||
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);
|
||||
} else {
|
||||
await _player.stop();
|
||||
}
|
||||
}
|
||||
|
||||
@override
|
||||
@@ -157,6 +206,7 @@ class Narrator extends ChangeNotifier {
|
||||
unawaited(s.cancel());
|
||||
}
|
||||
unawaited(_player.dispose());
|
||||
highlightedPassage.dispose();
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user