I'm trying to listen for voice data from a user within same voice channel the bot is in. I have an infinite loop, where I try to read voice data from AudioInputStream
. The problem is that every time I use TryReadFrame
method, it returns false
. And I have no idea why.
Here is my code:
private async Task ReadAsync() // This method is called only once, when bot found a voice stream
{
while (AudioStream.CanRead)
{
if (!AudioStream.TryReadFrame(default, out var frame))
{
//Log.Debug("No data found for user {User}.", User.Username);
await _writer!.StandardInput.BaseStream.WriteAsync(_silenceBuffer);
await _writer!.StandardInput.BaseStream.FlushAsync();
continue;
}
Log.Debug("Frame read from user {User}", User.Username);
// You probably don't need this part.
}
Log.Debug("Reading voice data of {User} finished.", User.Username);
}
I`ve read somewhere that Discord won't send any voice data unless bot sends at last one audio packet. So here is how I connect to a voice channel and do that:
_audioClient = await channel.ConnectAsync();
await using var outputStream = _audioClient.CreatePCMStream(AudioApplication.Mixed);
try
{
await outputStream.WriteAsync(new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, 0, 10);
await outputStream.FlushAsync();
}
catch (Exception e)
{
Log.Error(e, "Failed to send handshake voice data.");
}
I hope that someone can help me with this problem.
The issue was with the Discord.Net library itself and was resolved in this pull request. The voice UDP socket used to be misconfigured so could not receive any packets.