155 lines
4.5 KiB
Dart
155 lines
4.5 KiB
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) {
|
|
_subscriptions.add(
|
|
_player.playerStateStream.listen((_) => notifyListeners()),
|
|
);
|
|
_subscriptions.add(_player.positionStream.listen((_) => notifyListeners()));
|
|
}
|
|
final Map<String, dynamic> tracks;
|
|
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 =>
|
|
_player.playing && _player.processingState != ProcessingState.completed;
|
|
bool get finished => _player.processingState == ProcessingState.completed;
|
|
bool get paused => page.isNotEmpty && !playing && !finished && !loading;
|
|
Duration get position => _player.position;
|
|
String get label => 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(_player.duration ?? Duration.zero)}';
|
|
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 {
|
|
for (final cue in tracks[page]?['cues'] ?? []) {
|
|
if (position.inMilliseconds >= cue['startMs'] &&
|
|
position.inMilliseconds < cue['endMs']) {
|
|
return cue['speaker'] ?? tracks[page]?['speaker'] ?? '';
|
|
}
|
|
}
|
|
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();
|
|
return;
|
|
}
|
|
final generation = ++_generation;
|
|
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;
|
|
// PCM WAV avoids silent FLAC playback on the API 26 Huawei tablet.
|
|
await _player.setAsset('assets/audio/${tracks[id]['file']}');
|
|
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) {
|
|
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();
|
|
await _player.pause();
|
|
}
|
|
|
|
Future<void> stop() async {
|
|
cancelAutoplay();
|
|
++_generation;
|
|
page = '';
|
|
loading = false;
|
|
error = '';
|
|
notifyListeners();
|
|
await _player.stop();
|
|
}
|
|
|
|
@override
|
|
void dispose() {
|
|
_autoplay?.cancel();
|
|
for (final s in _subscriptions) {
|
|
unawaited(s.cancel());
|
|
}
|
|
unawaited(_player.dispose());
|
|
super.dispose();
|
|
}
|
|
}
|