reworked music controls, prune fix, cleanup, other fixes

This commit is contained in:
Master Kwoth
2016-01-31 19:51:27 +01:00
parent 40b64407f2
commit e869972127
7 changed files with 105 additions and 139 deletions

View File

@@ -9,15 +9,17 @@ namespace NadekoBot.Classes.Music {
public class MusicControls {
public bool NextSong = false;
public IAudioClient Voice;
public Channel VoiceChannel;
public bool Pause = false;
public List<StreamRequest> SongQueue = new List<StreamRequest>();
public StreamRequest CurrentSong;
public StreamRequest CurrentSong = null;
public bool IsPaused { get; internal set; } = false;
public bool Stopped { get; private set; }
public IAudioClient VoiceClient;
public Channel VoiceChannel;
public IAudioClient VoiceClient = null;
private readonly object _voiceLock = new object();
@@ -27,12 +29,13 @@ namespace NadekoBot.Classes.Music {
lock (_voiceLock) {
if (CurrentSong == null) {
if (SongQueue.Count > 0)
LoadNextSong();
LoadNextSong().Wait();
} else if (CurrentSong.State == StreamState.Completed || NextSong) {
NextSong = false;
LoadNextSong();
LoadNextSong().Wait();
}
}
await Task.Delay(1000);
}
@@ -43,22 +46,24 @@ namespace NadekoBot.Classes.Music {
VoiceChannel = voiceChannel;
}
public void LoadNextSong() {
lock (_voiceLock) {
CurrentSong?.Stop();
CurrentSong = null;
if (SongQueue.Count != 0) {
CurrentSong = SongQueue[0];
SongQueue.RemoveAt(0);
} else {
VoiceClient?.Disconnect();
VoiceClient = null;
return;
}
public async Task LoadNextSong() {
CurrentSong?.Stop();
CurrentSong = null;
if (SongQueue.Count != 0) {
CurrentSong = SongQueue[0];
SongQueue.RemoveAt(0);
} else {
VoiceClient?.Disconnect();
VoiceClient = null;
return;
}
try {
CurrentSong?.Start();
if (VoiceChannel == null)
VoiceChannel = CurrentSong.Channel;
if (VoiceClient == null)
VoiceClient = await NadekoBot.client.Audio().Join(VoiceChannel);
await CurrentSong.Start();
} catch (Exception ex) {
Console.WriteLine($"Starting failed: {ex}");
CurrentSong?.Stop();
@@ -69,7 +74,7 @@ namespace NadekoBot.Classes.Music {
lock (_voiceLock) {
Stopped = true;
foreach (var kvp in SongQueue) {
if(kvp != null)
if (kvp != null)
kvp.Stop();
}
SongQueue.Clear();
@@ -80,22 +85,6 @@ namespace NadekoBot.Classes.Music {
}
}
internal async Task<StreamRequest> CreateStreamRequest(CommandEventArgs e, string query, Channel voiceChannel) {
if (VoiceChannel == null)
throw new ArgumentNullException("Please join a voicechannel.");
StreamRequest sr = null;
if (VoiceClient == null) {
VoiceChannel = voiceChannel;
VoiceClient = await NadekoBot.client.Audio().Join(VoiceChannel);
}
sr = new StreamRequest(e, query, this);
lock (_voiceLock) {
SongQueue.Add(sr);
}
return sr;
}
internal bool TogglePause() => IsPaused = !IsPaused;
}
}

View File

@@ -30,7 +30,6 @@ namespace NadekoBot.Classes.Music {
public string Query { get; }
public string Title { get; internal set; } = String.Empty;
public IAudioClient VoiceClient { get; private set; }
private MusicStreamer musicStreamer = null;
public StreamState State => musicStreamer?.State ?? privateState;
@@ -38,23 +37,22 @@ namespace NadekoBot.Classes.Music {
public bool IsPaused => MusicControls.IsPaused;
private MusicControls MusicControls;
public MusicControls MusicControls;
public StreamRequest(CommandEventArgs e, string query, MusicControls mc) {
if (e == null)
throw new ArgumentNullException(nameof(e));
if (query == null)
throw new ArgumentNullException(nameof(query));
if (mc.VoiceClient == null)
throw new NullReferenceException($"{nameof(mc.VoiceClient)} is null, bot didn't join any server.");
this.MusicControls = mc;
this.VoiceClient = mc.VoiceClient;
this.Server = e.Server;
this.Query = query;
Task.Run(async () => await ResolveStreamLink());
Task.Run(() => ResolveStreamLink());
Console.WriteLine("6");
mc.SongQueue.Add(this);
}
private async Task ResolveStreamLink() {
private void ResolveStreamLink() {
VideoInfo video = null;
try {
if (OnResolving != null)
@@ -165,7 +163,6 @@ namespace NadekoBot.Classes.Music {
Arguments = $"-i {Url} -f s16le -ar 48000 -ac 2 pipe:1",
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardError = false,
CreateNoWindow = true,
WindowStyle = ProcessWindowStyle.Hidden,
});
@@ -282,13 +279,13 @@ namespace NadekoBot.Classes.Music {
break;
}
parent.VoiceClient.Send(voiceBuffer, 0, voiceBuffer.Length);
parent.MusicControls.VoiceClient.Send(voiceBuffer, 0, voiceBuffer.Length);
while (IsPaused) {
await Task.Delay(50);
}
}
parent.VoiceClient.Wait();
parent.MusicControls.VoiceClient.Wait();
Stop();
}