support for presearch next track

lowers the track change latency
This commit is contained in:
Kingkor Roy Tirtho 2022-07-19 12:49:47 +06:00
parent 1271a09e00
commit 68b440920c
2 changed files with 30 additions and 4 deletions

View File

@ -54,8 +54,8 @@ void useUpdateChecker(WidgetRef ref) {
showDialog(
context: context,
builder: (context) {
final url =
"https://github.com/KRTirtho/spotube/releases/tag/v${value.last}";
const url =
"https://spotube.netlify.app/other-downloads/stable-downloads";
return AlertDialog(
title: const Text("Spotube has an update"),
actions: [

View File

@ -58,6 +58,8 @@ class Playback extends PersistedChangeNotifier {
// internal stuff
final List<StreamSubscription> _subscriptions;
final _logger = getLogger(Playback);
// state of preSearch used inside [onPositionChanged]
bool _isPreSearching = false;
PlaybackStatus status;
@ -110,6 +112,26 @@ class Playback extends PersistedChangeNotifier {
currentDuration = await player.getDuration() ?? Duration.zero;
notifyListeners();
}
final currentTrackIndex =
playlist!.tracks.indexWhere((t) => t.id == track?.id);
// when the track progress is above 80%, track isn't the last
// and is not already fetch and nothing is fetching currently
if (pos.inSeconds > currentDuration.inSeconds * .8 &&
playlist != null &&
currentTrackIndex != playlist!.tracks.length - 1 &&
playlist!.tracks.elementAt(currentTrackIndex + 1)
is! SpotubeTrack &&
!_isPreSearching) {
_isPreSearching = true;
playlist!.tracks[currentTrackIndex + 1] = await toSpotubeTrack(
playlist!.tracks[currentTrackIndex + 1],
).then((v) {
_isPreSearching = false;
return v;
});
}
}),
]);
}());
@ -389,7 +411,9 @@ class Playback extends PersistedChangeNotifier {
(playlist!.trackIds.indexOf(track!.id!) + 1).toInt();
// checking if there's any track available forward
if (nextTrackIndex > (playlist?.tracks.length ?? 0) - 1) return;
await play(playlist!.tracks.elementAt(nextTrackIndex));
await play(playlist!.tracks.elementAt(nextTrackIndex)).then((_) {
playlist!.tracks[nextTrackIndex] = track!;
});
}
Future<void> seekBackward() async {
@ -398,7 +422,9 @@ class Playback extends PersistedChangeNotifier {
(playlist!.trackIds.indexOf(track!.id!) - 1).toInt();
// checking if there's any track available behind
if (prevTrackIndex < 0) return;
await play(playlist!.tracks.elementAt(prevTrackIndex));
await play(playlist!.tracks.elementAt(prevTrackIndex)).then((_) {
playlist!.tracks[prevTrackIndex] = track!;
});
}
@override