discord-net / Discord.Net

An unofficial .Net wrapper for the Discord API (https://discord.com/)
https://discordnet.dev
MIT License
3.34k stars 737 forks source link

[Bug]: Problem with sending voice through ffmpeg(spamming Sent Speaking)(It has been fixed before, but it still causes problems.) #2256

Open AGaL-Az-Yumika opened 2 years ago

AGaL-Az-Yumika commented 2 years ago

Check The Docs

Verify Issue Source

Check your intents

Description

Im sending audio using ffmpeg(ffmpeg version N-93129-g9e1e521393) and Discord.Net(Discord.Net 2.0.2-dev-01059). I'm sending audio from local .mp3 using ffmpeg with methods like below. When i'm sending first time from connection to voice channel - it is all ok. For the next times bot spamming Sent Speaking and his "circle" in discord is blinking.

private Process CreateStream(string path)
{
    return Process.Start(new ProcessStartInfo
    {
        FileName = "ffmpeg",
        Arguments = $"-hide_banner -loglevel panic -i \"{path}\" -ac 2 -f s16le -ar 48000 pipe:1",
        UseShellExecute = false,
        RedirectStandardOutput = true,
    });
}
using (var ffmpeg = CreateStream(path))
using (var output = ffmpeg.StandardOutput.BaseStream)
using (var discord = AudioClient.CreatePCMStream(AudioApplication.Mixed))
{
    try
    {
        IsSending = true;
        await AudioClient.SetSpeakingAsync(true);//just try to fix
        await output.CopyToAsync(discord, _token);//_token to stop sending audio
        return true;
    }
    catch (OperationCanceledException)
    {
        return true;
    }
    finally
    {
        await discord.FlushAsync();
        await AudioClient.SetSpeakingAsync(false);//just try to fix
        IsSending = false;
    }
}

Version

3.5.0

Working Version

No response

Logs

Null

Sample

No response

Packages

ffmpeg-n5.0-latest-win64-gpl-5.0.zip

Quget commented 10 months ago

I also stumbled upon this as well when wanting some text to speech.

When I force audio byte array to be of size 1.000.000 and above. It seems to be playing correctly. But then the bot will be silence for a while with short audio clips. I am using NAudio. Maybe it helps a bit knowing this.