Cleaned things for shuffle, but still has some deadlocks and crashes
This commit is contained in:
@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Text.RegularExpressions;
|
||||
using System.Threading;
|
||||
@ -34,7 +35,7 @@ namespace unison
|
||||
public class MPDHandler
|
||||
{
|
||||
private bool _connected;
|
||||
public string _version;
|
||||
private string _version;
|
||||
private int _currentVolume;
|
||||
private int _previousVolume;
|
||||
private bool _currentRandom;
|
||||
@ -43,6 +44,7 @@ namespace unison
|
||||
private bool _currentConsume;
|
||||
private double _currentTime;
|
||||
private double _totalTime;
|
||||
private IEnumerable<IMpdFile> _Playlist;
|
||||
|
||||
private MpdStatus _currentStatus;
|
||||
private IMpdFile _currentSong;
|
||||
@ -155,9 +157,7 @@ namespace unison
|
||||
|
||||
try
|
||||
{
|
||||
Debug.WriteLine("SafelySendCommandAsync => before command");
|
||||
IMpdMessage<T> response = await _commandConnection.SendAsync(command);
|
||||
Debug.WriteLine("SafelySendCommandAsync => after command");
|
||||
if (!response.IsResponseValid)
|
||||
{
|
||||
string mpdError = response.Response?.Result?.MpdError;
|
||||
@ -492,14 +492,26 @@ namespace unison
|
||||
public string GetVersion() => _version;
|
||||
public Statistics GetStats() => _stats;
|
||||
public double GetCurrentTime() => _currentTime;
|
||||
public IEnumerable<IMpdFile> GetPlaylist() => _Playlist;
|
||||
|
||||
public bool IsConnected() => _connected;
|
||||
public bool IsPlaying() => _currentStatus?.State == MpdState.Play;
|
||||
|
||||
public void Prev() => SendCommand(new PreviousCommand());
|
||||
public void Next() => SendCommand(new NextCommand());
|
||||
public void PlayPause() => SendCommand(new PauseResumeCommand());
|
||||
public bool CanPrevNext = true;
|
||||
|
||||
public void Prev()
|
||||
{
|
||||
if (CanPrevNext)
|
||||
SendCommand(new PreviousCommand());
|
||||
}
|
||||
|
||||
public void Next()
|
||||
{
|
||||
if (CanPrevNext)
|
||||
SendCommand(new NextCommand());
|
||||
}
|
||||
|
||||
public void PlayPause() => SendCommand(new PauseResumeCommand());
|
||||
public void Play(int pos) => SendCommand(new PlayCommand(pos));
|
||||
|
||||
public void Random() => SendCommand(new RandomCommand(!_currentRandom));
|
||||
@ -555,6 +567,15 @@ namespace unison
|
||||
SendCommand(commandList);
|
||||
}
|
||||
|
||||
public async Task QueryPlaylist() => _Playlist = await SafelySendCommandAsync(new PlaylistCommand());
|
||||
|
||||
public int GetPlaylistCount()
|
||||
{
|
||||
if (_Playlist == null)
|
||||
return 0;
|
||||
return _Playlist.ToArray().Count();
|
||||
}
|
||||
|
||||
public async void QueryStats()
|
||||
{
|
||||
Dictionary<string, string> Response = await SafelySendCommandAsync(new StatsCommand());
|
||||
|
134
Handlers/ShuffleHandler.cs
Normal file
134
Handlers/ShuffleHandler.cs
Normal file
@ -0,0 +1,134 @@
|
||||
using MpcNET.Commands.Database;
|
||||
using MpcNET.Commands.Queue;
|
||||
using MpcNET.Tags;
|
||||
using MpcNET.Types;
|
||||
using MpcNET.Types.Filters;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Diagnostics;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows;
|
||||
|
||||
namespace unison
|
||||
{
|
||||
class ShuffleHandler
|
||||
{
|
||||
private MPDHandler _mpd;
|
||||
public List<string> _songList { get; }
|
||||
public int AddedSongs = 0;
|
||||
|
||||
public ShuffleHandler()
|
||||
{
|
||||
_songList = new();
|
||||
_mpd = (MPDHandler)Application.Current.Properties["mpd"];
|
||||
}
|
||||
|
||||
private bool IsOnMainThread()
|
||||
{
|
||||
return Application.Current.Dispatcher.Thread == System.Threading.Thread.CurrentThread;
|
||||
}
|
||||
|
||||
public async Task GetSongsFromFilter(List<IFilter> filter)
|
||||
{
|
||||
Debug.WriteLine("[GetSongsFromFilterBefore] is on main thread => " + IsOnMainThread());
|
||||
await Task.Run(async() =>
|
||||
{
|
||||
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);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public async Task AddToQueueRandom(int SongNumber)
|
||||
{
|
||||
Debug.WriteLine("Add To Queue Random");
|
||||
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
int AddedSongs = 0;
|
||||
|
||||
Debug.WriteLine("song to add => " + SongNumber);
|
||||
for (int i = 0; i < SongNumber; i++)
|
||||
{
|
||||
// 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("got response");
|
||||
|
||||
await Task.Delay(1);
|
||||
if (Response.Count() > 0)
|
||||
{
|
||||
string filePath = Response.First().Path;
|
||||
_mpd.AddSong(filePath);
|
||||
Debug.WriteLine("song path => " + filePath);
|
||||
|
||||
if (i == 0)
|
||||
{
|
||||
if (!_mpd.IsPlaying())
|
||||
_mpd.Play(0);
|
||||
}
|
||||
|
||||
AddedSongs++;
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Debug.WriteLine("Add To Queue Random - finished");
|
||||
}
|
||||
|
||||
public async Task AddToQueueFilter(int SongNumber)
|
||||
{
|
||||
Debug.WriteLine("Add To Queue Filter");
|
||||
|
||||
await Task.Run(async () =>
|
||||
{
|
||||
int AddedSongs = 0;
|
||||
|
||||
Debug.WriteLine("song to add => " + SongNumber);
|
||||
// more requested songs than available => add everything
|
||||
if (SongNumber > _songList.Count)
|
||||
{
|
||||
foreach (string path in _songList)
|
||||
{
|
||||
await Task.Delay(1);
|
||||
_mpd.AddSong(path);
|
||||
Debug.WriteLine("song path => " + path);
|
||||
AddedSongs++;
|
||||
}
|
||||
}
|
||||
// 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)
|
||||
{
|
||||
int MaxIndex = new Random().Next(0, _songList.Count - 1);
|
||||
SongIndex.Add(MaxIndex);
|
||||
}
|
||||
|
||||
foreach (int index in SongIndex)
|
||||
_mpd.AddSong(_songList[index]);
|
||||
Debug.WriteLine("while - after");
|
||||
}
|
||||
});
|
||||
|
||||
Debug.WriteLine("Add To Queue Filter - finished");
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user