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

View File

@ -26,6 +26,34 @@ namespace NadekoBot.Services.Music
//_log.Warn(songUri); //_log.Warn(songUri);
this.SongUri = songUri; this.SongUri = songUri;
this._isLocal = isLocal; 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) private Process StartFFmpegProcess(string songUri, float skipTo = 0)
@ -65,16 +93,8 @@ namespace NadekoBot.Services.Music
var toReturn = new TaskCompletionSource<bool>(); var toReturn = new TaskCompletionSource<bool>();
var _ = Task.Run(() => var _ = Task.Run(() =>
{ {
try { 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;
////int maxLoopsPerSec = 25; ////int maxLoopsPerSec = 25;
//var sw = Stopwatch.StartNew(); //var sw = Stopwatch.StartNew();