Files
logic-puzzle/lib/services/narrator.dart
T
Hadrian Burkhardt a863b0ca93 Rearrange landscape puzzle layout
Landscape leaves now lay the puzzle out in two columns: the whole grid
keeps one column (square, sized to the page) with the heading, area
totals, hint/undo and notes beside it, and the leaf navigation folds
into a single row. The game leaf also keeps its actions in a compact
bottom bar, so the board and controls are visible without scrolling.

- Leaf gains an optional wideBody used on landscape pages.
- Board gains maxWidth, defaulting to the previous 350.
- Split the puzzle body into area, board and note builders so both
  layouts share the same controls.
- Add a landscape widget regression test and document the layout.

This snapshot also carries the pending 2.0.2 notebook work already in
the tree: read-ahead restore, bundled PCM WAV narration assets, the
audio consistency comment, version 2.0.2+11, and the related unit,
integration and script updates.
2026-10-31 06:21:36 +01:00

119 lines
3.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;
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;
// 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 {
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();
}
}