Increased music stability

This commit is contained in:
Kwoth 2016-10-19 10:22:37 +02:00
parent 2014fc8885
commit a3bf2d5efb

View File

@ -93,54 +93,54 @@ namespace NadekoBot.Modules.Music.Classes
var t = new Thread(new ThreadStart(async () => var t = new Thread(new ThreadStart(async () =>
{ {
try while (!Destroyed)
{ {
while (!Destroyed) try
{ {
try if (audioClient?.ConnectionState != ConnectionState.Connected)
{ {
if (audioClient?.ConnectionState != ConnectionState.Connected) if (audioClient != null)
{ try { await audioClient.DisconnectAsync().ConfigureAwait(false); } catch { }
audioClient = await PlaybackVoiceChannel.ConnectAsync().ConfigureAwait(false); audioClient = await PlaybackVoiceChannel.ConnectAsync().ConfigureAwait(false);
continue; continue;
}
CurrentSong = GetNextSong();
RemoveSongAt(0);
if (CurrentSong == null)
continue;
OnStarted(this, CurrentSong);
await CurrentSong.Play(audioClient, cancelToken);
OnCompleted(this, CurrentSong);
if (RepeatPlaylist)
AddSong(CurrentSong, CurrentSong.QueuerName);
if (RepeatSong)
AddSong(CurrentSong, 0);
}
catch (OperationCanceledException) { }
finally
{
if (!cancelToken.IsCancellationRequested)
{
SongCancelSource.Cancel();
}
SongCancelSource = new CancellationTokenSource();
cancelToken = SongCancelSource.Token;
CurrentSong = null;
await Task.Delay(300).ConfigureAwait(false);
} }
CurrentSong = GetNextSong();
RemoveSongAt(0);
if (CurrentSong == null)
continue;
OnStarted(this, CurrentSong);
await CurrentSong.Play(audioClient, cancelToken);
OnCompleted(this, CurrentSong);
if (RepeatPlaylist)
AddSong(CurrentSong, CurrentSong.QueuerName);
if (RepeatSong)
AddSong(CurrentSong, 0);
}
catch (OperationCanceledException) { }
catch (Exception ex)
{
Console.WriteLine("Music thread almost crashed.");
Console.WriteLine(ex);
}
finally
{
if (!cancelToken.IsCancellationRequested)
{
SongCancelSource.Cancel();
}
SongCancelSource = new CancellationTokenSource();
cancelToken = SongCancelSource.Token;
CurrentSong = null;
await Task.Delay(300).ConfigureAwait(false);
} }
}
catch (Exception ex) {
Console.WriteLine("Music thread crashed.");
Console.WriteLine(ex);
} }
})); }));