Migrate Lumen to Dart and Flutter 2.0
This commit is contained in:
@@ -0,0 +1,117 @@
|
||||
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;
|
||||
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
|
||||
? '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 '';
|
||||
}
|
||||
|
||||
Future<void> toggle(String id, double speed) async {
|
||||
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;
|
||||
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 {
|
||||
await _player.pause();
|
||||
}
|
||||
|
||||
Future<void> stop() async {
|
||||
++_generation;
|
||||
page = '';
|
||||
loading = false;
|
||||
error = '';
|
||||
notifyListeners();
|
||||
await _player.stop();
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
for (final s in _subscriptions) {
|
||||
unawaited(s.cancel());
|
||||
}
|
||||
unawaited(_player.dispose());
|
||||
super.dispose();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user