DiscordPHP icon indicating copy to clipboard operation
DiscordPHP copied to clipboard

Audio sometimes is not audible for a few seconds

Open SebastianSchoeps opened this issue 3 years ago • 1 comments

  • PHP Version:
    • 8.1.7
  • DiscordPHP Version:
    • 7.1.2

We have a bot that plays audio files into a channel. Everything works fine, except sometimes the audio is not audible for the first few seconds. After some seconds it becomes audible (it doesn't start delayed; it's just as it is muted for a few seconds). This happens randomly - the same audio file works several times and then the next time doesn't. It is also the same for all members in the channel.

I am not sure if this is an issue related to the framework or not - we are currently trying to learn Node JS to recreate this issue with DiscordJS...

We stripped down the functionality to a pretty straight-forward version that still has the problems:

$discord->on('ready', function(Discord $discord) {
    // Register command
    $command = new \Discord\Parts\Interactions\Command\Command($discord, [
        'name' => 'a',
        'description' => 'Play random audio', 
    ]);
    $discord->application->commands->save($command);

    $discord->on(Event::INTERACTION_CREATE, function(Interaction $interaction, Discord $discord) {
        if($interaction->data->name == 'a') {
            $file = 'counting';
            
            if($voiceClient = $discord->getVoiceClient($interaction->guild_id)) {
                if($voiceClient->isSpeaking()) {
                    $voiceClient->stop();
                }

                $resource = fopen($file, 'r');
                $voiceClient->playDCAStream($resource);
            }
            else {
                $voiceChannel = collect($interaction->guild->channels->toArray())->firstWhere('type', Channel::TYPE_VOICE);

                $discord->joinVoiceChannel($voiceChannel)
                    ->then(function(VoiceClient $voiceClient) use ($voiceChannel, $file) {
                        $resource = fopen($file, 'r');
                        $voiceClient->playDCAStream($resource);
                    });
            }

            $builder = MessageBuilder::new();
            $builder->setContent('Playing count from 1 to 10...');
            $interaction->respondWithMessage($builder);
        }
    });
});

The test bot is available at https://discord.com/api/oauth2/authorize?client_id=993817984487403540&permissions=2064&scope=bot%20applications.commands. You just use /a to play an audio file counting from 1 to 10 (the "1" is on very low volume, just ignore this). You don't have to wait until the audio is finished - just use /a again.

Can anyone point us in the direction how to trouble-shoot this?

Thanks in advance! Sebastian

SebastianSchoeps avatar Jul 05 '22 11:07 SebastianSchoeps

Our DiscordJS bot running on the same server did not show any issue like this. It seems like this has to do with how this framework does the streaming. Is anything known about issues like this?

SebastianSchoeps avatar Jul 13 '22 05:07 SebastianSchoeps

which operating system are you using?

key2peace avatar Oct 23 '22 08:10 key2peace

We are running on Ubuntu 22.04.

We have now switched to calling DiscordJS to play the audio which works fine.

SebastianSchoeps avatar Oct 24 '22 09:10 SebastianSchoeps