private async Task RestorePlaylist() { try { var playlist = await BackgroundTrackRepository.LoadPlaylist(); if (!playlist.Any()) { return; } var trackIds = playlist.Select(node => node.TrackId); var restoredplaylist = new SmartCollection <IMediaItem>(); foreach (int trackId in trackIds) { var trackItem = await Locator.MediaLibrary.LoadTrackById(trackId); if (trackItem != null) { restoredplaylist.Add(trackItem); } } if (!ApplicationSettingsHelper.Contains(nameof(CurrentMedia))) { return; } var index = (int)ApplicationSettingsHelper.ReadSettingsValue(nameof(CurrentMedia)); if (restoredplaylist.Any()) { if (index == -1) { // Background Audio was terminated // We need to reset the playlist, or set the current track 0. ApplicationSettingsHelper.SaveSettingsValue(nameof(CurrentMedia), 0); index = 0; } SetCurrentMediaPosition(index); } if (CurrentMedia >= restoredplaylist.Count || CurrentMedia == -1) { CurrentMedia = 0; } if (restoredplaylist.Any()) { await SetPlaylist(restoredplaylist, true, false, restoredplaylist[CurrentMedia]); } } catch (Exception e) { Debug.WriteLine("Failed to restore the playlist"); } }