mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-11-21 20:00:49 +01:00
fix(player): stabilize timestamps and restored-session seeking
Advance displayed timestamps at media-second boundaries between engine reports, respecting playback speed, pause, buffering, scrubbing and visibility. Retain and persist seeks on restored sessions before loading the source, including a zero-second target. Validation: 247 Flutter tests, targeted analyzer, formatter checks and git diff --check.
This commit is contained in:
1 parent
2ad05be4bd
commit
86ecaeb085
6 files changed
+398
-33
No files matched your search
@@ -2011,6 +2011,14 @@ class MusicPlayerHandler extends BaseAudioHandler
|
||||
await _autoMix.cancel();
|
||||
await _serializeSourceChange(() async {
|
||||
if (generation != _playRequestGeneration || _disposed) return;
|
||||
if (!_sourceReady) {
|
||||
// Restored sessions have no engine source yet. Retain the requested
|
||||
// position, including zero, for the first play instead of the old save.
|
||||
_pendingRestorePosition = position;
|
||||
_broadcastPosition(position, force: true);
|
||||
await _persistSession(position: position);
|
||||
return;
|
||||
}
|
||||
await _player.seek(position);
|
||||
if (generation != _playRequestGeneration || _disposed) return;
|
||||
_broadcastPosition(position, force: true);
|
||||
|
||||
@@ -0,0 +1,123 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:audio_service/audio_service.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
|
||||
/// Updates time labels at media-second boundaries between engine reports.
|
||||
class PlaybackTimeBuilder extends StatefulWidget {
|
||||
const PlaybackTimeBuilder({
|
||||
super.key,
|
||||
required this.position,
|
||||
required this.duration,
|
||||
required this.playback,
|
||||
required this.builder,
|
||||
this.preview,
|
||||
});
|
||||
|
||||
final Duration position;
|
||||
final Duration duration;
|
||||
final PlaybackState? playback;
|
||||
final Duration? preview;
|
||||
final Widget Function(BuildContext context, int seconds) builder;
|
||||
|
||||
@override
|
||||
State<PlaybackTimeBuilder> createState() => _PlaybackTimeBuilderState();
|
||||
}
|
||||
|
||||
class _PlaybackTimeBuilderState extends State<PlaybackTimeBuilder>
|
||||
with WidgetsBindingObserver {
|
||||
Timer? _timer;
|
||||
int _seconds = 0;
|
||||
bool _visible = false;
|
||||
bool _foreground = true;
|
||||
|
||||
bool _playing(PlaybackState? state) =>
|
||||
state != null &&
|
||||
state.playing &&
|
||||
state.processingState == AudioProcessingState.ready;
|
||||
|
||||
@override
|
||||
void initState() {
|
||||
super.initState();
|
||||
WidgetsBinding.instance.addObserver(this);
|
||||
_foreground = _isForeground(WidgetsBinding.instance.lifecycleState);
|
||||
}
|
||||
|
||||
bool _isForeground(AppLifecycleState? state) =>
|
||||
state != AppLifecycleState.paused &&
|
||||
state != AppLifecycleState.hidden &&
|
||||
state != AppLifecycleState.detached;
|
||||
|
||||
@override
|
||||
void didChangeDependencies() {
|
||||
super.didChangeDependencies();
|
||||
_visible = TickerMode.valuesOf(context).enabled;
|
||||
_sync();
|
||||
}
|
||||
|
||||
@override
|
||||
void didUpdateWidget(PlaybackTimeBuilder oldWidget) {
|
||||
super.didUpdateWidget(oldWidget);
|
||||
_sync(previous: oldWidget);
|
||||
}
|
||||
|
||||
@override
|
||||
void didChangeAppLifecycleState(AppLifecycleState state) {
|
||||
_foreground = _isForeground(state);
|
||||
setState(_sync);
|
||||
}
|
||||
|
||||
void _sync({PlaybackTimeBuilder? previous}) {
|
||||
_timer?.cancel();
|
||||
_timer = null;
|
||||
final playback = widget.playback;
|
||||
var position = widget.preview ?? playback?.position ?? widget.position;
|
||||
if (position < Duration.zero) position = Duration.zero;
|
||||
if (widget.duration > Duration.zero && position > widget.duration) {
|
||||
position = widget.duration;
|
||||
}
|
||||
final seconds = position.inSeconds;
|
||||
// A late sample near a second boundary must not roll the clock back.
|
||||
// Scrubbing, paused updates and larger position changes remain immediate.
|
||||
final smallCorrection =
|
||||
previous != null &&
|
||||
widget.preview == null &&
|
||||
previous.preview == null &&
|
||||
_playing(playback) &&
|
||||
_playing(previous.playback) &&
|
||||
playback!.speed == previous.playback!.speed &&
|
||||
(playback.position - previous.playback!.position).abs() <
|
||||
const Duration(milliseconds: 500);
|
||||
if (!smallCorrection || seconds >= _seconds) _seconds = seconds;
|
||||
|
||||
if (!_visible ||
|
||||
!_foreground ||
|
||||
widget.preview != null ||
|
||||
!_playing(playback) ||
|
||||
!playback!.speed.isFinite ||
|
||||
playback.speed <= 0 ||
|
||||
(widget.duration > Duration.zero && position >= widget.duration)) {
|
||||
return;
|
||||
}
|
||||
final remaining =
|
||||
Duration.microsecondsPerSecond -
|
||||
position.inMicroseconds % Duration.microsecondsPerSecond;
|
||||
final delay = Duration(microseconds: (remaining / playback.speed).ceil());
|
||||
_timer = Timer(
|
||||
delay < const Duration(milliseconds: 1)
|
||||
? const Duration(milliseconds: 1)
|
||||
: delay,
|
||||
() => setState(_sync),
|
||||
);
|
||||
}
|
||||
|
||||
@override
|
||||
void dispose() {
|
||||
_timer?.cancel();
|
||||
WidgetsBinding.instance.removeObserver(this);
|
||||
super.dispose();
|
||||
}
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) => widget.builder(context, _seconds);
|
||||
}
|
||||
@@ -25,6 +25,7 @@ import 'package:mornye/widgets/mornye_playback_button.dart';
|
||||
import 'package:mornye/widgets/mornye_playback_time.dart';
|
||||
import 'package:mornye/widgets/mornye_player_slider.dart';
|
||||
import 'package:mornye/widgets/playback_seek_slider.dart';
|
||||
import 'package:mornye/widgets/playback_time_builder.dart';
|
||||
|
||||
const _mornyeTimelinePadding = 28.0;
|
||||
|
||||
@@ -85,7 +86,7 @@ class PlayerPlaybackControls extends ConsumerWidget {
|
||||
builder: (context, preview, _) => Consumer(
|
||||
builder: (context, ref, quality) {
|
||||
final position = ref.watch(playbackPositionProvider);
|
||||
final elapsedSeconds = (preview ?? position).inSeconds;
|
||||
final playback = ref.watch(playbackStateProvider).value;
|
||||
return Padding(
|
||||
padding: EdgeInsets.symmetric(
|
||||
horizontal: edgeAligned
|
||||
@@ -138,36 +139,44 @@ class PlayerPlaybackControls extends ConsumerWidget {
|
||||
? MornyePlayerSlider.horizontalInset
|
||||
: 12,
|
||||
),
|
||||
child: Row(
|
||||
children: [
|
||||
if (mornye)
|
||||
MornyePlaybackTime(
|
||||
key: ValueKey('elapsed:$mediaId'),
|
||||
seconds: elapsedSeconds,
|
||||
style: timeStyle,
|
||||
)
|
||||
else
|
||||
Text(
|
||||
formatClock(elapsedSeconds),
|
||||
style: timeStyle,
|
||||
),
|
||||
Expanded(child: Center(child: quality)),
|
||||
if (mornye)
|
||||
MornyePlaybackTime(
|
||||
key: ValueKey('remaining:$mediaId'),
|
||||
// Subtract whole seconds so both labels roll together,
|
||||
// even when the track duration includes milliseconds.
|
||||
seconds: (duration.inSeconds - elapsedSeconds)
|
||||
.clamp(0, duration.inSeconds),
|
||||
remaining: true,
|
||||
style: timeStyle,
|
||||
)
|
||||
else
|
||||
Text(
|
||||
formatClock(duration.inSeconds),
|
||||
style: timeStyle,
|
||||
),
|
||||
],
|
||||
child: PlaybackTimeBuilder(
|
||||
key: ValueKey(mediaId),
|
||||
position: position,
|
||||
duration: duration,
|
||||
playback: playback,
|
||||
preview: preview,
|
||||
builder: (context, elapsedSeconds) => Row(
|
||||
children: [
|
||||
if (mornye)
|
||||
MornyePlaybackTime(
|
||||
key: ValueKey('elapsed:$mediaId'),
|
||||
seconds: elapsedSeconds,
|
||||
style: timeStyle,
|
||||
)
|
||||
else
|
||||
Text(
|
||||
formatClock(elapsedSeconds),
|
||||
style: timeStyle,
|
||||
),
|
||||
Expanded(child: Center(child: quality)),
|
||||
if (mornye)
|
||||
MornyePlaybackTime(
|
||||
key: ValueKey('remaining:$mediaId'),
|
||||
// Subtract whole seconds so both labels roll together,
|
||||
// even when the track duration includes milliseconds.
|
||||
seconds:
|
||||
(duration.inSeconds - elapsedSeconds)
|
||||
.clamp(0, duration.inSeconds),
|
||||
remaining: true,
|
||||
style: timeStyle,
|
||||
)
|
||||
else
|
||||
Text(
|
||||
formatClock(duration.inSeconds),
|
||||
style: timeStyle,
|
||||
),
|
||||
],
|
||||
),
|
||||
),
|
||||
),
|
||||
],
|
||||
|
||||
@@ -168,18 +168,25 @@ class _Session implements AudioSession {
|
||||
|
||||
class _SessionStore implements AppStateDatabase {
|
||||
int clears = 0;
|
||||
int? persistedPositionMs;
|
||||
|
||||
@override
|
||||
Future<Map<String, dynamic>?> getPlaybackSession() async => null;
|
||||
@override
|
||||
Future<void> savePlaybackSession(Map<String, dynamic> session) async {}
|
||||
Future<void> savePlaybackSession(Map<String, dynamic> session) async {
|
||||
persistedPositionMs = session['positionMs'] as int?;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<bool> updatePlaybackSessionState({
|
||||
required int index,
|
||||
required int positionMs,
|
||||
required bool shuffle,
|
||||
required String repeatMode,
|
||||
}) async => true;
|
||||
}) async {
|
||||
persistedPositionMs = positionMs;
|
||||
return true;
|
||||
}
|
||||
|
||||
@override
|
||||
Future<void> clearPlaybackSession() async => clears++;
|
||||
@@ -897,6 +904,34 @@ void main() {
|
||||
},
|
||||
);
|
||||
|
||||
for (final target in [Duration.zero, const Duration(seconds: 7)]) {
|
||||
test('seek after restore replaces the saved start with $target', () async {
|
||||
final decks = <_Deck>[];
|
||||
final store = _SessionStore();
|
||||
final handler = await _runtime(
|
||||
decks: decks,
|
||||
sessionDatabase: store,
|
||||
).initialize();
|
||||
await handler.restoreSession(
|
||||
items: [_track],
|
||||
index: 0,
|
||||
position: const Duration(seconds: 30),
|
||||
shuffle: false,
|
||||
);
|
||||
|
||||
await handler.seek(target);
|
||||
expect(handler.playbackState.value.updatePosition, target);
|
||||
expect(decks.single.sources, isEmpty);
|
||||
expect(decks.single.position, Duration.zero);
|
||||
expect(handler.playbackState.value.playing, isFalse);
|
||||
expect(store.persistedPositionMs, target.inMilliseconds);
|
||||
|
||||
await handler.play();
|
||||
expect(decks.single.position, target);
|
||||
expect(handler.playbackState.value.playing, isTrue);
|
||||
});
|
||||
}
|
||||
|
||||
test(
|
||||
'denied audio focus leaves a new source paused and allows retry',
|
||||
() async {
|
||||
|
||||
@@ -3,6 +3,7 @@ import 'dart:convert';
|
||||
import 'dart:ui' as ui;
|
||||
|
||||
import 'package:audio_service/audio_service.dart';
|
||||
import 'package:clock/clock.dart';
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter/rendering.dart'
|
||||
@@ -271,6 +272,42 @@ void main() {
|
||||
return frames;
|
||||
}
|
||||
|
||||
testWidgets('Mornye timestamps advance between native position reports', (
|
||||
tester,
|
||||
) async {
|
||||
final states = StreamController<PlaybackState>();
|
||||
addTearDown(states.close);
|
||||
await withClock(Clock(tester.binding.clock.now), () async {
|
||||
await pumpNowPlaying(
|
||||
tester,
|
||||
theme: MornyeTheme.build(Brightness.dark),
|
||||
playbackEvents: states.stream,
|
||||
);
|
||||
mediaItems.add(item('first'));
|
||||
states.add(
|
||||
PlaybackState(
|
||||
processingState: AudioProcessingState.ready,
|
||||
updatePosition: const Duration(seconds: 10),
|
||||
),
|
||||
);
|
||||
await settlePlayer(tester);
|
||||
states.add(
|
||||
PlaybackState(
|
||||
playing: true,
|
||||
processingState: AudioProcessingState.ready,
|
||||
updatePosition: const Duration(milliseconds: 10200),
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(milliseconds: 900));
|
||||
final elapsed = find.byKey(const ValueKey('elapsed:first'));
|
||||
expect(tester.widget<MornyePlaybackTime>(elapsed).seconds, 11);
|
||||
await tester.pump(const Duration(seconds: 1));
|
||||
expect(tester.widget<MornyePlaybackTime>(elapsed).seconds, 12);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
});
|
||||
});
|
||||
|
||||
for (final mornye in [false, true]) {
|
||||
testWidgets(
|
||||
'late duration refreshes the open player without reopening ($mornye)',
|
||||
|
||||
@@ -0,0 +1,153 @@
|
||||
import 'package:audio_service/audio_service.dart';
|
||||
import 'package:clock/clock.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mornye/widgets/playback_time_builder.dart';
|
||||
|
||||
PlaybackState _state(
|
||||
int milliseconds, {
|
||||
bool playing = true,
|
||||
double speed = 1,
|
||||
AudioProcessingState processing = AudioProcessingState.ready,
|
||||
}) => PlaybackState(
|
||||
playing: playing,
|
||||
processingState: processing,
|
||||
updatePosition: Duration(milliseconds: milliseconds),
|
||||
speed: speed,
|
||||
);
|
||||
|
||||
Widget _app(
|
||||
PlaybackState state, {
|
||||
Duration? preview,
|
||||
bool visible = true,
|
||||
Duration duration = const Duration(minutes: 3),
|
||||
}) => MaterialApp(
|
||||
home: TickerMode(
|
||||
enabled: visible,
|
||||
child: PlaybackTimeBuilder(
|
||||
position: state.updatePosition,
|
||||
duration: duration,
|
||||
playback: state,
|
||||
preview: preview,
|
||||
builder: (context, seconds) => Text('$seconds'),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
void main() {
|
||||
testWidgets('late native samples cannot roll a second backwards', (
|
||||
tester,
|
||||
) async {
|
||||
await withClock(Clock(tester.binding.clock.now), () async {
|
||||
await tester.pumpWidget(_app(_state(10200)));
|
||||
await tester.pump(const Duration(milliseconds: 800));
|
||||
expect(find.text('11'), findsOneWidget);
|
||||
|
||||
await tester.pumpWidget(_app(_state(10990)));
|
||||
expect(find.text('11'), findsOneWidget);
|
||||
expect(find.text('10'), findsNothing);
|
||||
await tester.pump(const Duration(milliseconds: 1010));
|
||||
expect(find.text('12'), findsOneWidget);
|
||||
await tester.pump(const Duration(seconds: 1));
|
||||
expect(find.text('13'), findsOneWidget);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('pause, buffering and scrubbing hold their exact positions', (
|
||||
tester,
|
||||
) async {
|
||||
await withClock(Clock(tester.binding.clock.now), () async {
|
||||
await tester.pumpWidget(_app(_state(5500)));
|
||||
await tester.pump(const Duration(milliseconds: 500));
|
||||
expect(find.text('6'), findsOneWidget);
|
||||
await tester.pumpWidget(_app(_state(6000, playing: false)));
|
||||
await tester.pump(const Duration(seconds: 3));
|
||||
expect(find.text('6'), findsOneWidget);
|
||||
await tester.pumpWidget(
|
||||
_app(_state(6000, processing: AudioProcessingState.buffering)),
|
||||
);
|
||||
await tester.pump(const Duration(seconds: 3));
|
||||
expect(find.text('6'), findsOneWidget);
|
||||
await tester.pumpWidget(_app(_state(30000), preview: Duration.zero));
|
||||
await tester.pump(const Duration(seconds: 3));
|
||||
expect(find.text('0'), findsOneWidget);
|
||||
await tester.pumpWidget(_app(_state(0, playing: false)));
|
||||
expect(find.text('0'), findsOneWidget);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('time follows playback speed and seeks immediately', (
|
||||
tester,
|
||||
) async {
|
||||
await withClock(Clock(tester.binding.clock.now), () async {
|
||||
await tester.pumpWidget(_app(_state(7200, speed: 2)));
|
||||
await tester.pump(const Duration(milliseconds: 400));
|
||||
expect(find.text('8'), findsOneWidget);
|
||||
await tester.pump(const Duration(seconds: 1));
|
||||
expect(find.text('10'), findsOneWidget);
|
||||
await tester.pumpWidget(_app(_state(10200, speed: 0.5)));
|
||||
await tester.pump(const Duration(milliseconds: 1600));
|
||||
expect(find.text('11'), findsOneWidget);
|
||||
await tester.pumpWidget(_app(_state(2000)));
|
||||
expect(find.text('2'), findsOneWidget);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('hidden clocks sleep and resynchronize when shown', (
|
||||
tester,
|
||||
) async {
|
||||
await withClock(Clock(tester.binding.clock.now), () async {
|
||||
final state = _state(200);
|
||||
await tester.pumpWidget(_app(state, visible: false));
|
||||
await tester.pump(const Duration(seconds: 3));
|
||||
expect(find.text('0'), findsOneWidget);
|
||||
await tester.pumpWidget(_app(state));
|
||||
expect(find.text('3'), findsOneWidget);
|
||||
await tester.pump(const Duration(milliseconds: 800));
|
||||
expect(find.text('4'), findsOneWidget);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('time stops at the duration boundary', (tester) async {
|
||||
await withClock(Clock(tester.binding.clock.now), () async {
|
||||
await tester.pumpWidget(
|
||||
_app(_state(9700), duration: const Duration(seconds: 10)),
|
||||
);
|
||||
await tester.pump(const Duration(seconds: 1));
|
||||
expect(find.text('10'), findsOneWidget);
|
||||
await tester.pump(const Duration(seconds: 3));
|
||||
expect(find.text('10'), findsOneWidget);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('background clocks resume from the current media time', (
|
||||
tester,
|
||||
) async {
|
||||
await withClock(Clock(tester.binding.clock.now), () async {
|
||||
try {
|
||||
await tester.pumpWidget(_app(_state(200)));
|
||||
await tester.pump(const Duration(milliseconds: 800));
|
||||
expect(find.text('1'), findsOneWidget);
|
||||
tester.binding.handleAppLifecycleStateChanged(AppLifecycleState.paused);
|
||||
await tester.pump();
|
||||
await tester.pump(const Duration(seconds: 2));
|
||||
expect(find.text('1'), findsOneWidget);
|
||||
tester.binding.handleAppLifecycleStateChanged(
|
||||
AppLifecycleState.resumed,
|
||||
);
|
||||
await tester.pump();
|
||||
expect(find.text('3'), findsOneWidget);
|
||||
} finally {
|
||||
tester.binding.handleAppLifecycleStateChanged(
|
||||
AppLifecycleState.resumed,
|
||||
);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
}
|
||||
});
|
||||
});
|
||||
}
|
||||
Reference in new issue
Block a user