Overo3
Overo3

Reputation: 21

Why does the VoiceNext D#+ library not work (not playing audio), despite not throwing any errors?

Using the DSharpPlus library, with conversion via ffmpeg, I have taken a YouTube video stream and converted it to PCM Stereo (the expected format), but when copying the stream to the buffer, no audio can be heard in the voice channel. Code for MusicCommands.cs:

using System.ComponentModel.DataAnnotations.Schema;
using System.Threading.Channels;
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
using DSharpPlus.VoiceNext;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using YoutubeExplode;
using YoutubeExplode.Videos.Streams;

namespace Nexus
{
    public class MusicCommands : ApplicationCommandModule
    {
        [SlashCommand("join","Join a voice channel")]
        public async Task JoinCommand(InteractionContext ctx)
        {
            DiscordChannel channel = ctx.Member.VoiceState?.Channel;
            await channel.ConnectAsync();
        }

        [SlashCommand("play","Play audio source")]
        public async Task PlayCommand(InteractionContext ctx, [Option("ytid","Youtube ID or link")] string path)
        {
            var vnext = ctx.Client.GetVoiceNext();
            var connection = vnext.GetConnection(ctx.Guild);

            var transmit = connection.GetTransmitSink();

            Task<Stream> audio = GetYoutubeVideoStream(path);
            dynamic result = await audio;
            await result.CopyToAsync(transmit);
            await result.DisposeAsync();
        }

        [SlashCommand("leave","Leave a voice channel")]
        public async Task LeaveCommand(InteractionContext ctx)
        {
            var vnext = ctx.Client.GetVoiceNext();
            var connection = vnext.GetConnection(ctx.Guild);

            connection.Disconnect();
        }
        private static async Task<Stream> GetYoutubeVideoStream(string ytID)
        {
            var youtube = new YoutubeClient();
            var streamManifest = await youtube.Videos.Streams.GetManifestAsync(ytID);
            var streamInfo = streamManifest.GetAudioOnlyStreams().GetWithHighestBitrate();
            var stream  = await youtube.Videos.Streams.GetAsync(streamInfo);
            var process = new Process
            {
                StartInfo =
                {
                    FileName = "ffmpeg",
                    Arguments = $@"-ac 2 -f s16le -ar 48000 pipe:1 -i -",
                    RedirectStandardOutput = true,
                    UseShellExecute = false
                }
            };

            var ffmpegIn = process.StandardInput.BaseStream;
            stream.CopyTo(ffmpegIn);
            var outputStream = process.StandardOutput.BaseStream;
            return outputStream;
        }
    }
}

According to the Discord voice docs, the S16LE PCM stereo 48000hz sample rate should be correct, and it doesn't throw any errors when using any of the commands, but it doesn't play any audio.

Upvotes: 0

Views: 71

Answers (0)

Related Questions