question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

IAudioClient.StreamCreated is not called

See original GitHub issue
Console.WriteLine("Action was called");

IAudioClient client = await channel.ConnectAsync();
client.StreamCreated += async (ulong id, AudioInStream stream) => {
  Console.WriteLine("StreamCreated was called");
  await Task.Delay(recordingTime);
  await channel.DisconnectAsync();
  stream.CopyTo(mem);
  doneListening = true;
};

Executing this code results in the following Console Output:

Action was called
14:03:50 Audio #1    Connecting
14:03:50 Audio #1    Unknown OpCode (Hello)
14:03:51 Audio #1    Connected

So StreamCreated was never called. Is receiving audio currently not supported? I couldn’t find any example code for this use case.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:1
  • Comments:5 (1 by maintainers)

github_iconTop GitHub Comments

1reaction
cmorley191commented, May 1, 2020

Aside from the issue of sending audio once in order to receive audio, there is another issue preventing StreamCreated from being called.

You will notice that StreamCreated does get called if, during the recording part of your code, a new user joins the voice channel (a stream gets created for this user). (you can also do this by you, the user, leaving the voice channel and rejoining after the bot has joined).

I conclude that StreamCreated is getting called, but it’s getting called before ConnectAsync() returns - StreamCreated is called by an internal method of the API triggered by the VOICE_STATE_UPDATED message being received from the discord api. This message is received before the channel finishes ConnectAsync(); that is, before you ever have access to the IAudioClient in order to add a StreamCreated event handler.

This is an oversight/bug of Discord.Net. There is currently no way to gain access to the AudioInStreams of users that were already connected to the voice channel that your bot joins, because there’s no way to add a StreamCreated event handler before these users’ streams are created (since ConnectAsync has not yet completed, and these users’ streams are created during ConnectAsync). You can only get access to AudioInStreams once users join the channel after your bot joins.

0reactions
martindevanscommented, May 25, 2020

As a third option, how about an additional optional parameter in ConnectAsync which is a function that runs after the object is created, but before any events are processed? Something like this:

using var c = await Channel.ConnectAsync(false, false, false, c => {
    c.StreamCreated += async (userId, stream) => {
        // Hello :D
    };
    c.StreamDestroyed += async userId => {
        // Goodbye :(
    };
});

This way, the event handlers are not subscribed too late and most existing code using these events will be very easy to migrate. It’s also not a breaking change to any code (as the event handlers would still exist).

Edit: I will be happy to implement this and submit a PR, if this approach is ok?

Read more comments on GitHub >

github_iconTop Results From Across the Web

IAudioClient::Start method (audioclient.h) - Win32
The audio stream is configured to use event-driven buffering, but the caller has not called IAudioClient::SetEventHandle to set the event ...
Read more >
StreamEvent - OpenTok.js Vonage Video API Developer
StreamEvent is an event that can have the type "streamCreated" or "streamDestroyed". These events are dispatched by the Session object when another client ......
Read more >
streamCreated not fired for existing participants - OpenVidu
I'm not receiving a streamCreated event for participants already connected so only older participants can see newer ones and not vice versa.
Read more >
I get an error while trying to call IAudioClient::GetService()
I get these errors when I'm trying to compile the program: image. I can get rid of error LNK2001 by using __uuidof(IChannelAudioVolume) ...
Read more >
C# (CSharp) IAudioClient Examples
These are the top rated real world C# (CSharp) examples of IAudioClient extracted from open ... StreamCreated += VoiceChannelAudioStreamCreated; client.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found