reafactoring

This commit is contained in:
2023-08-04 23:19:31 -03:00
parent 1ea940e03e
commit 73b0bac359
6 changed files with 82 additions and 66 deletions

View File

@@ -1,4 +1,5 @@
using System.Diagnostics;
using Discord.Audio;
using System.Diagnostics;
namespace Kasbot.Services.Internal
{
@@ -6,7 +7,57 @@ namespace Kasbot.Services.Internal
{
public AudioService() { }
public Process CreateStream()
public void StartAudioTask(Stream inputStream, IAudioClient outputAudioClient, Action<Stream> onStartAudio, Action<Task> onFinish)
{
var ffmpeg = CreateFFmpeg();
Task stdin = new Task(() =>
{
using (var output = inputStream)
{
try
{
output.CopyTo(ffmpeg.StandardInput.BaseStream);
ffmpeg.StandardInput.Close();
}
catch { }
finally
{
output.Flush();
}
}
});
Task stdout = new Task(() =>
{
using (var output = ffmpeg.StandardOutput.BaseStream)
using (var discord = outputAudioClient.CreatePCMStream(AudioApplication.Music))
{
try
{
onStartAudio.Invoke(ffmpeg.StandardOutput.BaseStream);
output.CopyTo(discord);
}
catch { }
finally
{
discord.Flush();
}
}
});
stdin.Start();
stdout.Start();
stdin.ContinueWith(onFinish);
stdout.ContinueWith(onFinish);
Task.WaitAll(stdin, stdout);
ffmpeg.Close();
}
private Process CreateFFmpeg()
{
var process = Process.Start(new ProcessStartInfo
{

View File

@@ -22,12 +22,14 @@ namespace Kasbot.Services.Internal
var playlistInfo = await youtube.Playlists.GetAsync(search);
await youtube.Playlists.GetVideosAsync(search).ForEachAsync(videoId =>
{
var media = new Media();
media.Name = videoId.Title;
media.Length = videoId.Duration ?? new TimeSpan(0);
media.VideoId = videoId.Id;
media.Message = message;
var media = new Media
{
Name = videoId.Title,
Length = videoId.Duration ?? new TimeSpan(0),
VideoId = videoId.Id,
Message = message,
Flags = new Flags()
};
collection.Medias.Add(media);
});