mirror of
https://github.com/zarzet/SpotiFLAC-Mobile.git
synced 2026-11-21 20:00:49 +01:00
fix(lyrics): keep line timers aligned with playback
Sample the current playback clock when a lyric boundary timer fires late and scale boundary delays by playback speed. Cover both themes at normal and accelerated playback speeds.
This commit is contained in:
1 parent
38659c024e
commit
5f4bb7ccee
4 files changed
+118
-4
No files matched your search
@@ -320,7 +320,9 @@ class _SyncedLyricsViewState extends ConsumerState<SyncedLyricsView>
|
||||
}
|
||||
|
||||
Duration get _displayPosition =>
|
||||
widget.seekPreview.value ?? ref.read(playbackPositionProvider);
|
||||
widget.seekPreview.value ??
|
||||
ref.read(playbackStateProvider).value?.position ??
|
||||
Duration.zero;
|
||||
|
||||
void _scheduleUserScrollReturn() {
|
||||
_userScrollIdleTimer?.cancel();
|
||||
@@ -447,9 +449,16 @@ class _SyncedLyricsViewState extends ConsumerState<SyncedLyricsView>
|
||||
}
|
||||
if (nextBoundary == null) return;
|
||||
final boundary = nextBoundary;
|
||||
_lineBoundaryTimer = Timer(boundary - position, () {
|
||||
final remaining = boundary - position;
|
||||
final speed = ref.read(playbackStateProvider).value?.speed ?? 1.0;
|
||||
final delay = speed.isFinite && speed > 0
|
||||
? Duration(microseconds: (remaining.inMicroseconds / speed).ceil())
|
||||
: remaining;
|
||||
_lineBoundaryTimer = Timer(delay, () {
|
||||
if (!mounted || !_visible || !_playing || _loading) return;
|
||||
_scheduleNextLine(boundary);
|
||||
// A delayed callback must catch up to the audio clock, rather than
|
||||
// treating its scheduled boundary as the current playback position.
|
||||
_scheduleNextLine(_displayPosition);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
+1
-1
@@ -303,7 +303,7 @@ packages:
|
||||
source: hosted
|
||||
version: "0.4.2"
|
||||
clock:
|
||||
dependency: transitive
|
||||
dependency: "direct dev"
|
||||
description:
|
||||
name: clock
|
||||
sha256: e51d50bca3217c9a9fa2b41a30e4a38971133f5f9ec7a3d57bae095007f1d28e
|
||||
|
||||
@@ -137,6 +137,7 @@ dev_dependencies:
|
||||
sdk: flutter
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
clock: 1.1.3
|
||||
analyzer: ^14.5.0
|
||||
flutter_lints: ^6.0.0
|
||||
build_runner: ^2.16.2
|
||||
|
||||
@@ -0,0 +1,104 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:audio_service/audio_service.dart';
|
||||
import 'package:clock/clock.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:mornye/l10n/l10n.dart';
|
||||
import 'package:mornye/providers/music_player_provider.dart';
|
||||
import 'package:mornye/theme/app_theme.dart';
|
||||
import 'package:mornye/theme/mornye_theme.dart';
|
||||
import 'package:mornye/utils/lyrics_parser.dart';
|
||||
import 'package:mornye/widgets/player_lyrics.dart';
|
||||
|
||||
void main() {
|
||||
for (final mornye in [false, true]) {
|
||||
for (final speed in [1.0, 1.5]) {
|
||||
testWidgets('late lyric timers catch up to playback ($mornye, $speed)', (
|
||||
tester,
|
||||
) async {
|
||||
var now = DateTime(2026);
|
||||
await withClock(Clock(() => now), () async {
|
||||
final playback = StreamController<PlaybackState>.broadcast();
|
||||
final preview = ValueNotifier<Duration?>(null);
|
||||
addTearDown(playback.close);
|
||||
addTearDown(preview.dispose);
|
||||
final theme = mornye
|
||||
? MornyeTheme.build(Brightness.dark)
|
||||
: AppTheme.dark();
|
||||
final lyrics = LyricsParser.parse('''
|
||||
[00:13.161]First phrase
|
||||
[00:26.065]Second phrase
|
||||
[00:39.067]Third phrase
|
||||
[00:51.938]Fourth phrase
|
||||
[04:34.977]Final phrase
|
||||
''');
|
||||
await tester.pumpWidget(
|
||||
ProviderScope(
|
||||
overrides: [
|
||||
playbackStateProvider.overrideWith((ref) => playback.stream),
|
||||
],
|
||||
child: MaterialApp(
|
||||
theme: theme,
|
||||
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
||||
supportedLocales: AppLocalizations.supportedLocales,
|
||||
home: Scaffold(
|
||||
body: SyncedLyricsView(
|
||||
lyrics: lyrics,
|
||||
seekPreview: preview,
|
||||
colorScheme: theme.colorScheme,
|
||||
isActive: true,
|
||||
showPronunciation: false,
|
||||
showTranslation: false,
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
playback.add(
|
||||
PlaybackState(
|
||||
playing: true,
|
||||
processingState: AudioProcessingState.ready,
|
||||
updatePosition: Duration.zero,
|
||||
speed: speed,
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
await tester.pump();
|
||||
|
||||
double emphasis(String phrase) => tester
|
||||
.widget<AnimatedOpacity>(
|
||||
find.ancestor(
|
||||
of: find.text(phrase),
|
||||
matching: find.byType(AnimatedOpacity),
|
||||
),
|
||||
)
|
||||
.opacity;
|
||||
|
||||
final startedAt = now;
|
||||
final firstBoundary = Duration(
|
||||
microseconds: (13161000 / speed).ceil(),
|
||||
);
|
||||
now = startedAt.add(firstBoundary);
|
||||
await tester.pump(firstBoundary);
|
||||
expect(emphasis('First phrase'), 1);
|
||||
|
||||
// The audio clock keeps moving while a timer callback is delayed.
|
||||
// No fresh position event is needed to catch up to the real song.
|
||||
now = startedAt.add(Duration(milliseconds: (45000 / speed).round()));
|
||||
await tester.pump(Duration(microseconds: (12904000 / speed).ceil()));
|
||||
expect(emphasis('Third phrase'), 1);
|
||||
expect(emphasis('First phrase'), lessThan(1));
|
||||
|
||||
now = now.add(Duration(milliseconds: (231000 / speed).round()));
|
||||
await tester.pump(const Duration(seconds: 7));
|
||||
expect(emphasis('Final phrase'), 1);
|
||||
expect(emphasis('Third phrase'), lessThan(1));
|
||||
expect(tester.takeException(), isNull);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user