Fix player position performance issue (#606)

This commit is contained in:
Piotr Rogowski 2023-08-01 15:44:00 +02:00 committed by GitHub
parent affdb57ecd
commit 3e0834f83c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 296 additions and 300 deletions

View File

@ -5,6 +5,7 @@
"danceability",
"instrumentalness",
"Mpris",
"riverpod",
"speechiness",
"Spotube",
"winget"

View File

@ -48,7 +48,6 @@ class PlayerControls extends HookConsumerWidget {
final playing =
useStream(audioPlayer.playingStream).data ?? audioPlayer.isPlaying;
final buffering = useStream(audioPlayer.bufferingStream).data ?? true;
final theme = Theme.of(context);
final isDominantColorDark = ThemeData.estimateBrightnessForColor(
@ -89,8 +88,7 @@ class PlayerControls extends HookConsumerWidget {
iconSize: compact ? 18 : 24,
);
return RepaintBoundary(
child: GestureDetector(
return GestureDetector(
behavior: HitTestBehavior.translucent,
onTap: () {
if (focusNode.canRequestFocus) {
@ -147,8 +145,7 @@ class PlayerControls extends HookConsumerWidget {
// than total duration. Keeping it resolved
value: progress.value.toDouble(),
secondaryTrackValue: bufferProgress,
onChanged:
playlist.isFetching == true || buffering
onChanged: playlist.isFetching == true
? null
: (v) {
progress.value = v;
@ -156,14 +153,12 @@ class PlayerControls extends HookConsumerWidget {
onChangeEnd: (value) async {
await audioPlayer.seek(
Duration(
seconds:
(value * duration.inSeconds).toInt(),
seconds: (value * duration.inSeconds).toInt(),
),
);
},
activeColor: sliderColor,
secondaryActiveColor:
sliderColor.withOpacity(0.2),
secondaryActiveColor: sliderColor.withOpacity(0.2),
inactiveColor: sliderColor.withOpacity(0.15),
),
),
@ -176,8 +171,7 @@ class PlayerControls extends HookConsumerWidget {
color: palette?.dominantColor?.bodyTextColor,
),
child: Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text("$currentMinutes:$currentSeconds"),
Text("$totalMinutes:$totalSeconds"),
@ -202,7 +196,7 @@ class PlayerControls extends HookConsumerWidget {
: context.l10n.shuffle_playlist,
icon: const Icon(SpotubeIcons.shuffle),
style: shuffled ? activeButtonStyle : buttonStyle,
onPressed: playlist.isFetching == true || buffering
onPressed: playlist.isFetching == true
? null
: () {
if (shuffled) {
@ -217,7 +211,7 @@ class PlayerControls extends HookConsumerWidget {
tooltip: context.l10n.previous_track,
icon: const Icon(SpotubeIcons.skipBack),
style: buttonStyle,
onPressed: playlist.isFetching == true || buffering
onPressed: playlist.isFetching == true
? null
: playlistNotifier.previous,
),
@ -249,15 +243,14 @@ class PlayerControls extends HookConsumerWidget {
tooltip: context.l10n.next_track,
icon: const Icon(SpotubeIcons.skipForward),
style: buttonStyle,
onPressed: playlist.isFetching == true || buffering
onPressed: playlist.isFetching == true
? null
: playlistNotifier.next,
),
StreamBuilder<PlaybackLoopMode>(
stream: audioPlayer.loopModeStream,
builder: (context, snapshot) {
final loopMode =
snapshot.data ?? PlaybackLoopMode.none;
final loopMode = snapshot.data ?? PlaybackLoopMode.none;
return IconButton(
tooltip: loopMode == PlaybackLoopMode.one
? context.l10n.loop_track
@ -273,7 +266,7 @@ class PlayerControls extends HookConsumerWidget {
loopMode == PlaybackLoopMode.all
? activeButtonStyle
: buttonStyle,
onPressed: playlist.isFetching == true || buffering
onPressed: playlist.isFetching == true
? null
: () async {
switch (await audioPlayer.loopMode) {
@ -300,7 +293,6 @@ class PlayerControls extends HookConsumerWidget {
),
),
),
),
);
}
}

View File

@ -62,7 +62,6 @@ class PlayerOverlay extends HookConsumerWidget {
child: AnimatedOpacity(
duration: const Duration(milliseconds: 250),
opacity: canShow ? 1 : 0,
child: RepaintBoundary(
child: Material(
type: MaterialType.transparency,
child: Column(
@ -124,8 +123,7 @@ class PlayerOverlay extends HookConsumerWidget {
? const SizedBox(
height: 20,
width: 20,
child:
CircularProgressIndicator(),
child: CircularProgressIndicator(),
)
: Icon(
playing
@ -133,8 +131,7 @@ class PlayerOverlay extends HookConsumerWidget {
: SpotubeIcons.play,
color: textColor,
),
onPressed:
Actions.handler<PlayPauseIntent>(
onPressed: Actions.handler<PlayPauseIntent>(
context,
PlayPauseIntent(ref),
),
@ -160,7 +157,6 @@ class PlayerOverlay extends HookConsumerWidget {
),
),
),
),
);
}
}

View File

@ -1,6 +1,5 @@
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:spotube/provider/proxy_playlist/proxy_playlist_provider.dart';
import 'package:spotube/services/audio_player/audio_player.dart';
({
@ -9,21 +8,29 @@ import 'package:spotube/services/audio_player/audio_player.dart';
Duration duration,
double bufferProgress
}) useProgress(WidgetRef ref) {
final playlist = ref.watch(ProxyPlaylistNotifier.provider);
final bufferProgress =
useStream(audioPlayer.bufferedPositionStream).data?.inSeconds ?? 0;
Duration audioPlayerDuration = Duration.zero;
Duration audioPlayerPosition = Duration.zero;
// Duration future is needed for getting the duration of the song
// as stream can be null when no event occurs (Mostly needed for android)
final durationFuture = useFuture(audioPlayer.duration);
final duration = useStream(audioPlayer.durationStream).data ??
durationFuture.data ??
Duration.zero;
audioPlayer.duration.then((value) {
if (value != null) {
audioPlayerDuration = value;
}
});
final positionFuture = useFuture(audioPlayer.position);
final position = useState<Duration>(positionFuture.data ?? Duration.zero);
audioPlayer.position.then((value) {
if (value != null) {
audioPlayerPosition = value;
}
});
final position = useState<Duration>(audioPlayerPosition);
final duration =
useStream(audioPlayer.durationStream).data ?? audioPlayerDuration;
final sliderMax = duration.inSeconds;
final sliderValue = position.value.inSeconds;
@ -38,7 +45,7 @@ import 'package:spotube/services/audio_player/audio_player.dart';
lastPosition = event;
position.value = event;
}).cancel;
}, []);
}, [audioPlayerPosition, audioPlayerDuration]);
return (
progressStatic: