Skip to content

Commit

Permalink
fix: track not skipping to next even when source is available
Browse files Browse the repository at this point in the history
  • Loading branch information
KRTirtho committed Aug 4, 2023
1 parent 0e5d546 commit 0b7affd
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 42 deletions.
4 changes: 3 additions & 1 deletion lib/components/playlist/playlist_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,9 @@ class PlaylistCard extends HookConsumerWidget {
playlistNotifier.addCollection(playlist.id!);
tracks.value = fetchedTracks;
} finally {
updating.value = false;
if (context.mounted) {
updating.value = false;
}
}
},
onAddToQueuePressed: () async {
Expand Down
2 changes: 1 addition & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ if (DesktopTools.platform.isAndroid) {
runApp(
DevicePreview(
availableLocales: L10n.all,
enabled: !kReleaseMode && DesktopTools.platform.isDesktop,
enabled: false,
data: const DevicePreviewData(
isEnabled: false,
orientation: Orientation.portrait,
Expand Down
12 changes: 0 additions & 12 deletions lib/provider/dbus_provider.dart

This file was deleted.

40 changes: 13 additions & 27 deletions lib/provider/proxy_playlist/proxy_playlist_provider.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import 'dart:async';
import 'dart:convert';

import 'package:async/async.dart';
import 'package:catcher/catcher.dart';
import 'package:collection/collection.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
Expand Down Expand Up @@ -68,7 +69,7 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
() async {
notificationService = await AudioServices.create(ref, this);

(String, List<SkipSegment>)? currentSegments;
({String source, List<SkipSegment> segments})? currentSegments;
bool isFetchingSegments = false;
audioPlayer.activeSourceChangedStream.listen((newActiveSource) async {
final newActiveTrack =
Expand Down Expand Up @@ -110,14 +111,14 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>

bool isPreSearching = false;

listenTo60Percent(percent) async {
listenTo2Percent(int percent) async {
if (isPreSearching ||
audioPlayer.currentSource == null ||
audioPlayer.nextSource == null) return;

try {
isPreSearching = true;

// TODO: Make repeat mode sensitive changes later
final oldTrack =
mapSourcesToTracks([audioPlayer.nextSource!]).firstOrNull;
final track = await ensureSourcePlayable(audioPlayer.nextSource!);
Expand All @@ -126,12 +127,12 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
state = state.copyWith(tracks: mergeTracks([track], state.tracks));
if (currentSegments == null ||
(oldTrack?.id != null &&
currentSegments!.$1 != oldTrack!.id!) &&
currentSegments!.source != oldTrack!.id!) &&
!isFetchingSegments) {
isFetchingSegments = true;
currentSegments = (
audioPlayer.currentSource!,
await getAndCacheSkipSegments(
source: audioPlayer.currentSource!,
segments: await getAndCacheSkipSegments(
track.ytTrack.id,
),
);
Expand All @@ -147,47 +148,32 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier<ProxyPlaylist>
}
} finally {
isPreSearching = false;

/// Sometimes fetching can take a lot of time, so we need to check
/// if next source is playable or not at 99% progress. If not, then
/// it'll be paused automatically
///
/// After fetching the nextSource and replacing it, we need to check
/// if the player is paused or not. If it is paused, then we need to
/// resume it to skip to next track
if (audioPlayer.isPaused) {
if (percent > 98 && !audioPlayer.isPlaying) {
await audioPlayer.resume();
}
}
}

audioPlayer.percentCompletedStream(60).listen(listenTo60Percent);

// player stops at 99% if nextSource is still not playable
audioPlayer.percentCompletedStream(99).listen((_) async {
if (audioPlayer.nextSource == null ||
isPlayable(audioPlayer.nextSource!)) return;
await audioPlayer.pause();
});
audioPlayer.percentCompletedStream(2).listen(listenTo2Percent);

audioPlayer.positionStream.listen((position) async {
if (preferences.searchMode == SearchMode.youtubeMusic ||
!preferences.skipNonMusic) return;

if (currentSegments == null ||
currentSegments!.$1 != state.activeTrack!.id! &&
currentSegments!.source != state.activeTrack!.id! &&
!isFetchingSegments) {
isFetchingSegments = true;
currentSegments = (
audioPlayer.currentSource!,
await getAndCacheSkipSegments(
source: audioPlayer.currentSource!,
segments: await getAndCacheSkipSegments(
(state.activeTrack as SpotubeTrack).ytTrack.id,
),
);
isFetchingSegments = false;
}

final (_, segments) = currentSegments!;
final (source: _, :segments) = currentSegments!;
if (segments.isEmpty) return;

for (final segment in segments) {
Expand Down
3 changes: 2 additions & 1 deletion lib/services/audio_services/linux_audio_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import 'dart:io';
import 'package:dbus/dbus.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';

import 'package:spotube/provider/dbus_provider.dart';
import 'package:spotube/models/spotube_track.dart';
import 'package:spotube/provider/proxy_playlist/proxy_playlist.dart';
import 'package:spotube/provider/proxy_playlist/proxy_playlist_provider.dart';
Expand All @@ -12,6 +11,8 @@ import 'package:spotube/services/audio_player/loop_mode.dart';
import 'package:spotube/utils/type_conversion_utils.dart';
import 'package:window_manager/window_manager.dart';

final dbus = DBusClient.session();

class _MprisMediaPlayer2 extends DBusObject {
/// Creates a new object to expose on [path].
_MprisMediaPlayer2() : super(DBusObjectPath('/org/mpris/MediaPlayer2')) {
Expand Down

0 comments on commit 0b7affd

Please sign in to comment.