more musicplayer fixes/rewrites

This commit is contained in:
Kwoth 2016-07-21 12:42:17 +02:00
parent b506064d37
commit c6af3a6c3e

View File

@ -22,7 +22,6 @@ namespace NadekoBot.Modules.Music.Classes
{ {
Resolving, Resolving,
Queued, Queued,
Buffering, //not using it atm
Playing, Playing,
Completed Completed
} }
@ -35,7 +34,7 @@ namespace NadekoBot.Modules.Music.Classes
public IReadOnlyCollection<Song> Playlist => playlist; public IReadOnlyCollection<Song> Playlist => playlist;
private readonly object playlistLock = new object(); private readonly object playlistLock = new object();
public Song CurrentSong { get; set; } = default(Song); public Song CurrentSong { get; private set; }
private CancellationTokenSource SongCancelSource { get; set; } private CancellationTokenSource SongCancelSource { get; set; }
private CancellationToken cancelToken { get; set; } private CancellationToken cancelToken { get; set; }
@ -103,20 +102,22 @@ namespace NadekoBot.Modules.Music.Classes
{ {
try try
{ {
if (audioClient.State != ConnectionState.Connected) if (audioClient?.State != ConnectionState.Connected)
{ {
audioClient = await PlaybackVoiceChannel.JoinAudio(); audioClient = await PlaybackVoiceChannel.JoinAudio();
continue; continue;
} }
var song = CurrentSong; CurrentSong = GetNextSong();
RemoveSongAt(0);
if (song == null) if (CurrentSong == null)
continue; continue;
try try
{ {
await song.Play(audioClient, cancelToken); OnStarted(this, CurrentSong);
await CurrentSong.Play(audioClient, cancelToken);
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {
@ -124,17 +125,19 @@ namespace NadekoBot.Modules.Music.Classes
SongCancelSource = new CancellationTokenSource(); SongCancelSource = new CancellationTokenSource();
cancelToken = SongCancelSource.Token; cancelToken = SongCancelSource.Token;
} }
OnCompleted(this, song); OnCompleted(this, CurrentSong);
if (RepeatPlaylist) if (RepeatPlaylist)
AddSong(song, song.QueuerName); AddSong(CurrentSong, CurrentSong.QueuerName);
if (RepeatSong) if (RepeatSong)
AddSong(song, 0); AddSong(CurrentSong, 0);
} }
finally finally
{ {
await Task.Delay(300).ConfigureAwait(false); await Task.Delay(300).ConfigureAwait(false);
CurrentSong = null;
} }
} }
} }
@ -149,18 +152,20 @@ namespace NadekoBot.Modules.Music.Classes
public void Next() public void Next()
{ {
Paused = false; actionQueue.Enqueue(() =>
SongCancelSource.Cancel(); {
Paused = false;
SongCancelSource.Cancel();
});
} }
public void Stop() public void Stop()
{ {
actionQueue.Enqueue(() => actionQueue.Enqueue(() =>
{ {
playlist.Clear();
CurrentSong = null;
RepeatPlaylist = false; RepeatPlaylist = false;
RepeatSong = false; RepeatSong = false;
playlist.Clear();
if (!SongCancelSource.IsCancellationRequested) if (!SongCancelSource.IsCancellationRequested)
SongCancelSource.Cancel(); SongCancelSource.Cancel();
}); });
@ -245,9 +250,10 @@ namespace NadekoBot.Modules.Music.Classes
{ {
actionQueue.Enqueue(() => actionQueue.Enqueue(() =>
{ {
playlist.Clear(); RepeatPlaylist = false;
RepeatSong = false;
Destroyed = true; Destroyed = true;
CurrentSong = null; playlist.Clear();
if (!SongCancelSource.IsCancellationRequested) if (!SongCancelSource.IsCancellationRequested)
SongCancelSource.Cancel(); SongCancelSource.Cancel();
audioClient.Disconnect(); audioClient.Disconnect();