Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

When IAudioClient ConnectAsync is created, null is returned #2803

Open
TABURELTER opened this issue Dec 3, 2023 · 3 comments
Open

When IAudioClient ConnectAsync is created, null is returned #2803

TABURELTER opened this issue Dec 3, 2023 · 3 comments

Comments

@TABURELTER
Copy link

I'm trying to broadcast audio via ffmpeg and after I create a connection to a voice channel, IAudioClient is assigned null because of which I can't broadcast audio.

` public async Task JoinChannel(SocketMessage msg, string URL, IVoiceChannel? channel = null)
{
channel ??= (msg.Author as IGuildUser)?.VoiceChannel;
if (channel == null) { await msg.Channel.SendMessageAsync("Пользователь должен находиться в голосовом канале."); return; }
try
{
using (var ffmpeg = CreateStream(URL))
using (var output = ffmpeg.StandardOutput.BaseStream)
{
AudioOutStream audioClient;
audioClient = channel.ConnectAsync(false, false, true).Result.CreatePCMStream(AudioApplication.Music);
try
{
await output.CopyToAsync(audioClient);
}
finally
{
await audioClient.FlushAsync();
}
}
}
catch (Exception e)
{
Console.WriteLine(e);
throw;
}
}

    private Process CreateStream(string path)
    {
        try
        {
            return Process.Start(new ProcessStartInfo
            {
                FileName = "ffmpeg/ffmpeg.exe",
                Arguments = $"-hide_banner -loglevel panic -i \"{path}\" -ac 2 -f s16le -ar 48000 pipe:1",
                UseShellExecute = false,
                RedirectStandardOutput = true,
            });
        }
        catch (Exception e)
        {
            Console.WriteLine("Process CreateStream");
            Console.WriteLine(e);
        }

        return null;
    }
}

`

@TABURELTER
Copy link
Author

sorry for the code repetition, I suck at writing github questions.

@Quantam-Studios
Copy link
Contributor

I'm trying to broadcast audio via ffmpeg and after I create a connection to a voice channel, IAudioClient is assigned null because of which I can't broadcast audio.

public async Task JoinChannel(SocketMessage msg, string URL, IVoiceChannel? channel = null)
{ 
    channel ??= (msg.Author as IGuildUser)?.VoiceChannel; 
    if (channel == null)
    { 
        await msg.Channel.SendMessageAsync("Пользователь должен находиться в голосовом канале."); 
        return;
    } 

    try
    { 
         using (var ffmpeg = CreateStream(URL)) 
         using (var output = ffmpeg.StandardOutput.BaseStream) 
         { 
              AudioOutStream audioClient; audioClient = channel.ConnectAsync(false, false, true).Result.CreatePCMStream(AudioApplication.Music); 
              try 
              { 
                   await output.CopyToAsync(audioClient); 
              } 
              finally 
              { 
                   await audioClient.FlushAsync(); 
              }
         } 
    } 
    catch (Exception e) 
    { 
         Console.WriteLine(e);
         throw; 
    }
}
    private Process CreateStream(string path)
    {
        try
        {
            return Process.Start(new ProcessStartInfo
            {
                FileName = "ffmpeg/ffmpeg.exe",
                Arguments = $"-hide_banner -loglevel panic -i \"{path}\" -ac 2 -f s16le -ar 48000 pipe:1",
                UseShellExecute = false,
                RedirectStandardOutput = true,
            });
        }
        catch (Exception e)
        {
            Console.WriteLine("Process CreateStream");
            Console.WriteLine(e);
        }

        return null;
    }
}

Formatted for the sake of everyone.

@Quantam-Studios
Copy link
Contributor

I can't reproduce this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants