240 lines
6.8 KiB
Dart
240 lines
6.8 KiB
Dart
import 'browser_assets.dart';
|
|
import 'web_narration_stub.dart'
|
|
if (dart.library.js_interop) 'web_narration_web.dart';
|
|
|
|
import 'dart:async';
|
|
|
|
import 'package:flutter/foundation.dart';
|
|
import 'package:just_audio/just_audio.dart';
|
|
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()),
|
|
);
|
|
_subscriptions.add(
|
|
_player.positionStream.listen((position) {
|
|
final state = (position.inSeconds, passage, speaker);
|
|
if (state == _positionState) return;
|
|
_positionState = state;
|
|
notifyListeners();
|
|
}),
|
|
);
|
|
}
|
|
(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();
|
|
}
|
|
|
|
Map<String, dynamic> tracks;
|
|
void replaceTracks(Map<String, dynamic> value) {
|
|
unawaited(stop());
|
|
tracks = value;
|
|
}
|
|
|
|
final _player = AudioPlayer();
|
|
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 => 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 => kIsWeb ? _web!.position : _player.position;
|
|
Duration get duration =>
|
|
kIsWeb ? _web!.duration : (_player.duration ?? Duration.zero);
|
|
String get label => error.isNotEmpty
|
|
? 'Play this reading'
|
|
: loading
|
|
? 'Opening recording…'
|
|
: playing
|
|
? 'Pause recording'
|
|
: finished
|
|
? 'Replay recording'
|
|
: paused
|
|
? 'Resume recording'
|
|
: 'Play this reading';
|
|
String _time(Duration d) =>
|
|
'${d.inMinutes}:${(d.inSeconds % 60).toString().padLeft(2, '0')}';
|
|
String get status => error.isNotEmpty
|
|
? error
|
|
: page.isEmpty
|
|
? 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(duration)}';
|
|
String get passage {
|
|
if (page.isEmpty || finished) return '';
|
|
for (final cue in tracks[page]?['cues'] ?? []) {
|
|
if (position.inMilliseconds >= cue['startMs'] &&
|
|
position.inMilliseconds < cue['endMs']) {
|
|
return cue['text'];
|
|
}
|
|
}
|
|
return '';
|
|
}
|
|
|
|
String get speaker {
|
|
var current = tracks[page]?['speaker'] as String? ?? '';
|
|
for (final cue in tracks[page]?['cues'] ?? []) {
|
|
if (position.inMilliseconds < cue['startMs']) break;
|
|
current = cue['speaker'] as String? ?? current;
|
|
}
|
|
// Hold the role through its pause, instead of flashing back to Narrator.
|
|
return current;
|
|
}
|
|
|
|
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) {
|
|
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();
|
|
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']}';
|
|
// 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);
|
|
}
|
|
await _player.setSpeed(speed);
|
|
if (generation != _generation) return;
|
|
loading = false;
|
|
notifyListeners();
|
|
unawaited(
|
|
_player.play().catchError((Object e) {
|
|
if (generation != _generation) return;
|
|
unawaited(_player.pause());
|
|
error = 'This recording could not be played. You can still read the entry.';
|
|
notifyListeners();
|
|
}),
|
|
);
|
|
} catch (e) {
|
|
if (generation == _generation) {
|
|
error =
|
|
'This recording could not be played. You can still read the entry.';
|
|
loading = false;
|
|
page = '';
|
|
notifyListeners();
|
|
}
|
|
}
|
|
}
|
|
|
|
Future<void> pause() async {
|
|
cancelAutoplay();
|
|
if (kIsWeb) {
|
|
_web!.pause();
|
|
return;
|
|
}
|
|
await _player.pause();
|
|
}
|
|
|
|
Future<void> stop() async {
|
|
cancelAutoplay();
|
|
++_generation;
|
|
page = '';
|
|
loading = false;
|
|
error = '';
|
|
notifyListeners();
|
|
if (kIsWeb) {
|
|
_web!.stop();
|
|
} else {
|
|
await _player.stop();
|
|
}
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_disposed = true;
|
|
_generation++;
|
|
_autoplay?.cancel();
|
|
_web?.dispose();
|
|
for (final s in _subscriptions) {
|
|
unawaited(s.cancel());
|
|
}
|
|
unawaited(_player.dispose());
|
|
highlightedPassage.dispose();
|
|
super.dispose();
|
|
}
|
|
}
|