Fixed a bug where spamming .n would crash the music player, closes #1372

This commit is contained in:
Master Kwoth 2017-07-10 21:21:45 +02:00
parent 70f0f6af44
commit 9a744172a9
2 changed files with 104 additions and 81 deletions

View File

@ -163,25 +163,26 @@ namespace NadekoBot.Services.Music
if (data.Song != null)
{
_log.Info("Starting");
using (var b = new SongBuffer(await data.Song.Uri(), "", data.Song.ProviderType == Database.Models.MusicType.Local))
{
_log.Info("Created buffer, buffering...");
AudioOutStream pcm = null;
SongBuffer b = null;
try
{
var bufferTask = b.StartBuffering(cancelToken);
var timeout = Task.Delay(10000);
if (Task.WhenAny(bufferTask, timeout) == timeout)
{
_log.Info("Buffering failed due to a timeout.");
continue;
}
else if (!bufferTask.Result)
{
_log.Info("Buffering failed due to a cancel or error.");
continue;
}
_log.Info("Buffered. Getting audio client...");
b = new SongBuffer(await data.Song.Uri(), "", data.Song.ProviderType == Database.Models.MusicType.Local);
//_log.Info("Created buffer, buffering...");
//var bufferTask = b.StartBuffering(cancelToken);
//var timeout = Task.Delay(10000);
//if (Task.WhenAny(bufferTask, timeout) == timeout)
//{
// _log.Info("Buffering failed due to a timeout.");
// continue;
//}
//else if (!bufferTask.Result)
//{
// _log.Info("Buffering failed due to a cancel or error.");
// continue;
//}
//_log.Info("Buffered. Getting audio client...");
var ac = await GetAudioClient();
_log.Info("Got Audio client");
if (ac == null)
@ -232,6 +233,9 @@ namespace NadekoBot.Services.Music
pcm.Dispose();
}
if (b != null)
b.Dispose();
OnCompleted?.Invoke(this, data.Song);
if (_bytesSent == 0 && !cancel)
@ -241,7 +245,6 @@ namespace NadekoBot.Services.Music
_log.Info("Song removed because it can't play");
}
}
}
try
{
//if repeating current song, just ignore other settings,

View File

@ -26,6 +26,34 @@ namespace NadekoBot.Services.Music
//_log.Warn(songUri);
this.SongUri = songUri;
this._isLocal = isLocal;
try
{
this.p = StartFFmpegProcess(SongUri, 0);
var t = Task.Run(() =>
{
this.p.BeginErrorReadLine();
this.p.ErrorDataReceived += P_ErrorDataReceived;
this.p.WaitForExit();
});
this._outStream = this.p.StandardOutput.BaseStream;
}
catch (System.ComponentModel.Win32Exception)
{
_log.Error(@"You have not properly installed or configured FFMPEG.
Please install and configure FFMPEG to play music.
Check the guides for your platform on how to setup ffmpeg correctly:
Windows Guide: https://goo.gl/OjKk8F
Linux Guide: https://goo.gl/ShjCUo");
}
catch (OperationCanceledException) { }
catch (InvalidOperationException) { } // when ffmpeg is disposed
catch (Exception ex)
{
_log.Info(ex);
}
}
private Process StartFFmpegProcess(string songUri, float skipTo = 0)
@ -65,16 +93,8 @@ namespace NadekoBot.Services.Music
var toReturn = new TaskCompletionSource<bool>();
var _ = Task.Run(() =>
{
try {
this.p = StartFFmpegProcess(SongUri, 0);
var t = Task.Run(() =>
try
{
this.p.BeginErrorReadLine();
this.p.ErrorDataReceived += P_ErrorDataReceived;
this.p.WaitForExit();
});
this._outStream = this.p.StandardOutput.BaseStream;
////int maxLoopsPerSec = 25;
//var sw = Stopwatch.StartNew();