fix(desktop): keep the output open across a gapless song change

The load hook of the queued song set gapless-audio back to no before mpv
had decided whether that song keeps the open device, so the device was
closed and reopened at every song change. The libmpv build used on
Windows closes it without playing what is still queued, which drops the
end of the previous song.

The option now goes back to no once the queued song has started.
This commit is contained in:
zarzet committed 2026-11-18 00:35:44 +07:00
1 parent f4cc3866a2
commit cff77a0dbf
3 files changed
+43 -5

No files matched your search

+28 -2
View File
@@ -495,9 +495,12 @@ class MediaKitPlaybackDeck implements PlaybackDeck, QueuedPlaybackDeck {
// The on_load hook blocks event delivery. Use the synchronous native
// property setter rather than media_kit's event-acknowledged volume API.
await _engine.setProperty('volume', _mpvVolume.toString());
// With no following entry, completion must wait for the audible tail.
await _engine.setProperty('gapless-audio', 'no');
if (_disposed || token != _token || revision != _nextRevision) return;
unawaited(
_drainOnceStarted(token, revision).catchError(
(Object error) => _log.w('Gapless drain setting: $error'),
),
);
onStarted();
});
return !_disposed && token == _token && revision == _nextRevision;
@@ -507,6 +510,29 @@ class MediaKitPlaybackDeck implements PlaybackDeck, QueuedPlaybackDeck {
}
}
/// With no following entry, completion must wait for the audible tail, so
/// `gapless-audio` goes back to `no`. mpv decides whether the queued song
/// keeps the open device only when that song's first audio is ready, after
/// the load hook returns: `no` set inside the hook closes and reopens the
/// device at every song change. `seeking` reads `yes` until that decision
/// is made, and nothing while the file still loads.
Future<void> _drainOnceStarted(int token, int revision) async {
for (var attempt = 0; attempt < _startPolls; attempt++) {
if (_disposed || token != _token || revision != _nextRevision) return;
if (await _engine.getProperty('seeking') == 'no') break;
await Future<void>.delayed(_startPoll);
}
// A newer queued song or source owns the setting by now.
if (_disposed || token != _token || revision != _nextRevision) return;
await _engine.setProperty('gapless-audio', 'no');
}
static const _startPoll = Duration(milliseconds: 20);
/// Ten seconds: a song that has not started by then loses only the
/// retained device, not its ending.
static const _startPolls = 500;
@override
Future<void> clearNextSource() async {
_nextRevision++;
+7 -2
View File
@@ -98,8 +98,8 @@ void main() {
engine.completedEvents.add(true);
await Future<void>.delayed(Duration.zero);
expect(events, isEmpty);
await engine.advance();
await Future<void>.delayed(Duration.zero);
await engine.advance(started: false);
await _settle();
expect(events, ['next']);
expect(deck.state, PlaybackDeckState.playing);
expect(engine.volume, closeTo(100 * 0.7368063, 0.001));
@@ -107,6 +107,11 @@ void main() {
engine.calls.where((call) => call.first == 'open'),
hasLength(opens),
);
// mpv keeps the open device only if the option still reads `weak`
// when the queued song's first audio is ready.
expect(engine.properties['gapless-audio'], 'weak');
engine.finishStart();
await _settle();
expect(engine.properties['gapless-audio'], 'no');
engine.completedEvents.add(true);
await Future<void>.delayed(Duration.zero);
+8 -1
View File
@@ -47,15 +47,22 @@ class FakeMpvEngine implements MpvEngine, MpvPlaylistEngine {
queuedStart = null;
}
Future<void> advance() async {
/// Moves to the queued song. mpv reports `seeking` until the song's first
/// audio is ready; [started] false leaves it there for [finishStart].
Future<void> advance({bool started = true}) async {
final start = queuedStart;
queuedStart = null;
queuedSource = null;
position = Duration.zero;
properties.remove('seeking');
await start?.call();
properties['seeking'] = 'yes';
durationEvents.add(openDuration!);
if (started) finishStart();
}
void finishStart() => properties['seeking'] = 'no';
@override
Stream<bool> get completed => completedEvents.stream;
@override