Looks like most of shuffle issues are resolved

This commit is contained in:
2022-11-11 17:57:50 +01:00
parent 06207d9791
commit 5a43a1284e
5 changed files with 152 additions and 95 deletions

View File

@ -67,7 +67,7 @@ namespace unison
private MpcConnection _commandConnection;
private IPEndPoint _mpdEndpoint;
private CancellationTokenSource _cancelCommand;
public CancellationTokenSource _cancelCommand;
private CancellationTokenSource _cancelConnect;
public MPDHandler()

View File

@ -75,7 +75,6 @@ namespace unison.Handlers
public async Task<List<StationInfo>> AdvancedSearch(AdvancedSearchOptions options)
{
return await _radioBrowser.Search.AdvancedAsync(options);
}
}

View File

@ -1,14 +1,16 @@
using MpcNET.Commands.Database;
using MpcNET.Commands.Queue;
using MpcNET.Tags;
using MpcNET.Types;
using MpcNET.Types.Filters;
using System;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using MpcNET.Commands.Database;
using MpcNET.Commands.Queue;
using MpcNET.Commands.Reflection;
using MpcNET.Tags;
using MpcNET.Types;
using MpcNET.Types.Filters;
namespace unison
{
@ -24,109 +26,129 @@ namespace unison
_mpd = (MPDHandler)Application.Current.Properties["mpd"];
}
private bool IsOnMainThread()
/*private bool IsOnMainThread()
{
return Application.Current.Dispatcher.Thread == System.Threading.Thread.CurrentThread;
}
}*/
public async Task GetSongsFromFilter(List<IFilter> filter)
public async Task GetSongsFromFilter(List<IFilter> filter, CancellationToken token)
{
Debug.WriteLine("[GetSongsFromFilterBefore] is on main thread => " + IsOnMainThread());
await Task.Run(async() =>
//Debug.WriteLine("[GetSongsFromFilterBefore] is on main thread => " + IsOnMainThread());
//await Task.Run(async() =>
//{
//Debug.WriteLine("[GetSongsFromFilterAfter] is on main thread => " + IsOnMainThread());
if (token.IsCancellationRequested)
return;
_songList.Clear();
int song = _mpd.GetStats().Songs;
Debug.WriteLine("before search command / song == " + song);
//var response = await Task.Run(async () =>
//{
/*return*/ IEnumerable<IMpdFile> response = await _mpd.SafelySendCommandAsync(new SearchCommand(filter, 0, song + 1));
//});
Debug.WriteLine("got response => " + response.Count());
foreach (IMpdFile file in response)
{
Debug.WriteLine("[GetSongsFromFilterAfter] is on main thread => " + IsOnMainThread());
_songList.Clear();
int song = _mpd.GetStats().Songs;
IEnumerable<IMpdFile> response = await _mpd.SafelySendCommandAsync(new SearchCommand(filter, 0, song + 1));
Debug.WriteLine("got response => " + response.Count());
foreach (IMpdFile file in response)
{
_songList.Add(file.Path);
Debug.WriteLine(file.Path);
}
});
_songList.Add(file.Path);
//Debug.WriteLine(file.Path);
}
//});
}
public async Task AddToQueueRandom(int SongNumber)
public async Task AddToQueueRandom(int SongNumber, CancellationToken token)
{
Debug.WriteLine("Add To Queue Random");
await Task.Run(async () =>
if (token.IsCancellationRequested)
return;
//await Task.Run(async () =>
//{
int AddedSongs = 0;
Debug.WriteLine("song to add => " + SongNumber);
for (int i = 0; i < SongNumber; i++)
{
int AddedSongs = 0;
// generate random number
int song = new Random().Next(0, _mpd.GetStats().Songs - 1);
Debug.WriteLine("song " + song + " - song total " + _mpd.GetStats().Songs);
IEnumerable<IMpdFile> Response = await _mpd.SafelySendCommandAsync(new SearchCommand(new FilterTag(MpdTags.Title, "", FilterOperator.Contains), song, song + 1));
Debug.WriteLine("song to add => " + SongNumber);
for (int i = 0; i < SongNumber; i++)
Debug.WriteLine("got response");
await Task.Delay(1);
if (Response.Count() > 0)
{
// generate random number
int song = new Random().Next(0, _mpd.GetStats().Songs - 1);
Debug.WriteLine("song " + song + " - song total " + _mpd.GetStats().Songs);
IEnumerable<IMpdFile> Response = await _mpd.SafelySendCommandAsync(new SearchCommand(new FilterTag(MpdTags.Title, "", FilterOperator.Contains), song, song + 1));
string filePath = Response.First().Path;
_mpd.AddSong(filePath);
Debug.WriteLine("song path => " + filePath);
Debug.WriteLine("got response");
await Task.Delay(1);
if (Response.Count() > 0)
if (i == 0)
{
string filePath = Response.First().Path;
_mpd.AddSong(filePath);
Debug.WriteLine("song path => " + filePath);
if (i == 0)
{
if (!_mpd.IsPlaying())
_mpd.Play(0);
}
AddedSongs++;
if (!_mpd.IsPlaying())
_mpd.Play(0);
}
AddedSongs++;
}
});
}
//});
Debug.WriteLine("Add To Queue Random - finished");
}
public async Task AddToQueueFilter(int SongNumber)
public async Task AddToQueueFilter(int SongNumber, CancellationToken token)
{
Debug.WriteLine("Add To Queue Filter");
await Task.Run(async () =>
if (token.IsCancellationRequested)
return;
//await Task.Run(async () =>
//{
int AddedSongs = 0;
Debug.WriteLine("song to add => " + SongNumber);
// more requested songs than available => add everything
if (SongNumber > _songList.Count)
{
int AddedSongs = 0;
var commandList = new CommandList();
Debug.WriteLine("song to add => " + SongNumber);
// more requested songs than available => add everything
if (SongNumber > _songList.Count)
foreach (string path in _songList)
{
foreach (string path in _songList)
{
await Task.Delay(1);
_mpd.AddSong(path);
Debug.WriteLine("song path => " + path);
AddedSongs++;
}
commandList.Add(new AddCommand(path));
AddedSongs++;
}
// more available songs than requested =>
// we add unique indexes until we reach the requested amount
else
Trace.WriteLine("Added " + AddedSongs + " songs");
await _mpd.SafelySendCommandAsync(commandList);
}
// more available songs than requested =>
// we add unique indexes until we reach the requested amount
else
{
HashSet<int> SongIndex = new();
Debug.WriteLine("while - before");
while (SongIndex.Count < SongNumber)//_songList.Count - 1)//SongNumber)
{
HashSet<int> SongIndex = new();
Debug.WriteLine("while - before");
while (SongIndex.Count < SongNumber)
{
int MaxIndex = new Random().Next(0, _songList.Count - 1);
SongIndex.Add(MaxIndex);
}
int MaxIndex = new Random().Next(0, _songList.Count - 1);
SongIndex.Add(MaxIndex);
}
Debug.WriteLine("while - middle");
var commandList = new CommandList();
foreach (int index in SongIndex)
_mpd.AddSong(_songList[index]);
Debug.WriteLine("while - after");
}
});
foreach (int index in SongIndex)
commandList.Add(new AddCommand(_songList[index]));
await _mpd.SafelySendCommandAsync(commandList);
//_mpd.AddSong(_songList[index]);
Debug.WriteLine("while - after");
}
//});
Debug.WriteLine("Add To Queue Filter - finished");
}