diff --git a/lib/provider/proxy_playlist/proxy_playlist_provider.dart b/lib/provider/proxy_playlist/proxy_playlist_provider.dart index 927abd6a7..5b29ecf08 100644 --- a/lib/provider/proxy_playlist/proxy_playlist_provider.dart +++ b/lib/provider/proxy_playlist/proxy_playlist_provider.dart @@ -15,6 +15,7 @@ import 'package:spotube/provider/proxy_playlist/proxy_playlist.dart'; import 'package:spotube/provider/user_preferences_provider.dart'; import 'package:spotube/services/audio_player/audio_player.dart'; import 'package:spotube/services/audio_services/audio_services.dart'; +import 'package:spotube/services/youtube.dart'; import 'package:spotube/utils/persisted_state_notifier.dart'; import 'package:spotube/utils/type_conversion_utils.dart'; @@ -215,29 +216,35 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier int initialIndex = 0, bool autoPlay = false, }) async { - tracks = blacklist.filter(tracks).toList() as List; - final addableTrack = await SpotubeTrack.fetchFromTrack( - tracks.elementAt(initialIndex), - preferences, - ); + try { + tracks = blacklist.filter(tracks).toList() as List; + final addableTrack = await SpotubeTrack.fetchFromTrack( + tracks.elementAt(initialIndex), + preferences, + ); - state = state.copyWith( - tracks: mergeTracks([addableTrack], tracks), - active: initialIndex, - ); + print('addableTrack: $addableTrack'); - await notificationService.addTrack(addableTrack); + state = state.copyWith( + tracks: mergeTracks([addableTrack], tracks), + active: initialIndex, + ); - await audioPlayer.openPlaylist( - state.tracks.map(makeAppropriateSource).toList(), - initialIndex: initialIndex, - autoPlay: autoPlay, - ); + await notificationService.addTrack(addableTrack); - await storeTrack( - tracks.elementAt(initialIndex), - addableTrack, - ); + await audioPlayer.openPlaylist( + state.tracks.map(makeAppropriateSource).toList(), + initialIndex: initialIndex, + autoPlay: autoPlay, + ); + + await storeTrack( + tracks.elementAt(initialIndex), + addableTrack, + ); + } catch (e) { + print('Error: $e'); + } } Future jumpTo(int index) async { @@ -404,14 +411,16 @@ class ProxyPlaylistNotifier extends PersistedStateNotifier } @override - onInit() { + onInit() async { if (state.tracks.isEmpty) return null; - return load( - state.tracks, - initialIndex: state.active ?? 0, - autoPlay: false, - ); + if (await PipedSpotube.initialized) { + await load( + state.tracks, + initialIndex: state.active ?? 0, + autoPlay: false, + ); + } } @override diff --git a/lib/services/youtube.dart b/lib/services/youtube.dart index 4a2091f3f..1f37d463d 100644 --- a/lib/services/youtube.dart +++ b/lib/services/youtube.dart @@ -1,8 +1,13 @@ +import 'dart:async'; + import 'package:piped_client/piped_client.dart'; PipedClient _defaultClient = PipedClient(); class PipedSpotube { + static final Completer _initialized = Completer(); + static Future get initialized => _initialized.future; + /// Checks for a working instance of piped.video /// /// To distribute the load, in each startup it randomizes public instances @@ -15,6 +20,7 @@ class PipedSpotube { try { await client.streams("dQw4w9WgXcQ"); _defaultClient = client; + _initialized.complete(true); break; } catch (e) { continue;