Skip to content

Commit

Permalink
Merge pull request #386 from Fooxboy/385
Browse files Browse the repository at this point in the history
fix: task canceled exception being thrown on prev/next track switches
  • Loading branch information
Fooxboy authored Jun 12, 2024
2 parents 712f61d + 3ea0208 commit 2389c3d
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 15 deletions.
22 changes: 20 additions & 2 deletions MusicX/Services/Player/PlayerService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -169,7 +169,22 @@ private async Task PlayTrackAsync(PlaylistTrack track, TimeSpan? position = null
}
}

if (!(await Task.WhenAll(_mediaSources.Select(b => b.OpenWithMediaPlayerAsync(player, track, _tokenSource.Token)))).Any())
var allSourcesTask = Task.WhenAll(_mediaSources.Select(b => b.OpenWithMediaPlayerAsync(player, track, _tokenSource.Token)));
try
{
await allSourcesTask;
}
catch
{
// await unwraps AggregateException into only the first exception,
// but we need to make sure that all exceptions are cancel ones
if (allSourcesTask.IsCanceled || allSourcesTask.Exception?.InnerExceptions.All(b => b is OperationCanceledException) is true)
return; // canceled

throw;
}

if (!allSourcesTask.Result.Any(b => b)) // no sources picked up this track
{
await NextTrack();
return;
Expand Down Expand Up @@ -357,7 +372,10 @@ async Task LoadMore()
if (CurrentIndex + 1 > Tracks.Count - 1)
{
if (CurrentPlaylist?.CanLoad == true)
{
await LoadMore();
nextTrack = Tracks.ElementAtOrDefault(CurrentIndex + 1);
}
}
else
{
Expand Down Expand Up @@ -848,7 +866,7 @@ public void ShuffleTracks(int? startingIndex)
if (startingIndex is > 0)
{
(Items[startingIndex.Value], Items[0]) = (Items[0], Items[startingIndex.Value]);
shuffledIndexes[0] = 0;
shuffledIndexes[0] = startingIndex.Value;
}

_originalTracks = Items.ToList();
Expand Down
22 changes: 16 additions & 6 deletions MusicX/Services/Player/Playlists/VkBlockPlaylist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace MusicX.Services.Player.Playlists;
public class VkBlockPlaylist : PlaylistBase<string>
{
private readonly VkService _vkService;
private string? _nextFrom;

[ActivatorUtilitiesConstructor]
// ReSharper disable once RedundantOverload.Global
Expand All @@ -21,16 +22,25 @@ public VkBlockPlaylist(VkService vkService, string blockId, bool loadOther = tru
{
_vkService = vkService;
Data = blockId;
_canLoad = loadOther;
_firstLoad = loadOther;
}

public override IAsyncEnumerable<PlaylistTrack> LoadAsync()
public override async IAsyncEnumerable<PlaylistTrack> LoadAsync()
{
_canLoad = false;
return _vkService.LoadFullAudiosAsync(Data).Select(TrackExtensions.ToTrack);
if (_firstLoad || _nextFrom is not null)
{
var response = await _vkService.GetSectionAsync(Data, _nextFrom);
_nextFrom = response.Section?.NextFrom;
foreach (var item in response.Audios)
{
yield return item.ToTrack();
}
}

_firstLoad = false;
}

private bool _canLoad;
public override bool CanLoad => _canLoad;
private bool _firstLoad;
public override bool CanLoad => _nextFrom is not null || _firstLoad;
public override string Data { get; }
}
34 changes: 27 additions & 7 deletions MusicX/Services/Player/Playlists/VkPlaylistPlaylist.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ namespace MusicX.Services.Player.Playlists;
public class VkPlaylistPlaylist : PlaylistBase<PlaylistData>
{
private readonly VkService _vkService;
private int _offset;
private int _count;

private const int LoadCount = 40;

public VkPlaylistPlaylist(VkService vkService, PlaylistData data)
{
Expand All @@ -21,25 +25,41 @@ public VkPlaylistPlaylist(VkService vkService, PlaylistData data)

public override async IAsyncEnumerable<PlaylistTrack> LoadAsync()
{
_canLoad = false;

var (id, ownerId, accessKey) = Data;
var playlist = await _vkService.LoadFullPlaylistAsync(id, ownerId, accessKey);

var trackPlaylist = new Playlist
{
Id = Data.PlaylistId,
OwnerId = Data.OwnerId,
AccessKey = Data.AccessKey
};

foreach (var audio in playlist.Audios)
if (_firstLoad)
{
var playlist = await _vkService.GetPlaylistAsync(LoadCount, id, accessKey, ownerId);

_count = (int)playlist.Playlist.Count;
_offset = playlist.Audios.Count;

foreach (var audio in playlist.Audios)
{
yield return audio.ToTrack(trackPlaylist);
}

_firstLoad = false;
yield break;
}

var response = await _vkService.AudioGetAsync(id, ownerId, accessKey, _offset, LoadCount);

_offset += response.Items.Count;

foreach (var audio in response.Items)
{
yield return audio.ToTrack(trackPlaylist);
}
}

private bool _canLoad = true;
public override bool CanLoad => _canLoad;
private bool _firstLoad = true;
public override bool CanLoad => _firstLoad || _offset < _count;
public override PlaylistData Data { get; }
}
4 changes: 4 additions & 0 deletions MusicX/Services/Player/Sources/BoomMediaSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ public override async Task<bool> OpenWithMediaPlayerAsync(MediaPlayer player, Pl

RegisterSourceObjectReference(player, rtMediaSource);
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception e)
{
_logger.Error(e, "Failed to use winrt decoder for boom media source");
Expand Down
4 changes: 4 additions & 0 deletions MusicX/Services/Player/Sources/VkMediaSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public override async Task<bool> OpenWithMediaPlayerAsync(MediaPlayer player, Pl

RegisterSourceObjectReference(player, rtMediaSource);
}
catch (OperationCanceledException)
{
throw;
}
catch (Exception e)
{
_logger.Error(e, "Failed to use winrt decoder for vk media source");
Expand Down

0 comments on commit 2389c3d

Please sign in to comment.