Reputation: 36
I am building a bot for my discord server to play the audio YouTube videos as I haven't found a reliable bot online.
The bot connects to my voice channel after I type the !play {url} command but immediately leaves even if the URL is valid.
My code is below:
[Command("play", RunMode = RunMode.Async)]
public async Task play(string url) {
IVoiceChannel channel = (Context.User as IVoiceState).VoiceChannel;
IAudioClient client = await channel.ConnectAsync();
var output = CreateStream(url).StandardOutput.BaseStream;
var stream = client.CreatePCMStream(AudioApplication.Music, 128 * 1024);
output.CopyToAsync(stream);
stream.FlushAsync().ConfigureAwait(false);
}
private Process CreateStream(string url) {
Process currentsong = new Process();
currentsong.StartInfo = new ProcessStartInfo {
FileName = "cmd.exe",
Arguments = $"/C youtube-dl -o - {url} | ffmpeg -i pipe:0 -ac 2 -f s16le -ar 48000 pipe:1",
UseShellExecute = false,
RedirectStandardOutput = true,
CreateNoWindow = true
};
currentsong.Start();
return currentsong;
}
I have tried using just ffmpeg with a file on my PC which was hard coded in, but I had the same result and the bot left the voice channel as soon as it connected.
I did verify that both ffmpeg and youtube-dl were working by running the commands in a cmd window and they both ran fine.
Upvotes: 0
Views: 4339
Reputation: 156
ffmpeg is not the only requirement for voice. You need the voice encoding and encryption libraries that Discord uses. I suggest joining the Discord.Net channel inside the Discord API server, which is where we are always at to answer questions such as these, as well as where we have tons of resources such as the links to where pre compiled versions of those libraries are.
https://discord.gg/discord-api Channel is #dotnet_discord-net
Upvotes: 2