discord-php-helpers/voice
Helpers for using DiscordPHP voice: connect to voice channels, handle voice gateway/UDP, and stream audio from PHP 8+ CLI apps. Built on ReactPHP/event loops with promises; intended for bot developers needing voice support.
Start by installing the package via Composer: composer require discord-php/voice. This package is a low-level voice client extension for DiscordPHP — it’s not a standalone library. You must first use team-reflex/discord-php as your core Discord client and attach voice capabilities via this package.
The first use case is joining a voice channel to play or stream audio (e.g., music bot, voice assistant, notification bot). Minimal setup:
Discord instance with appropriate intents (GUILD_VOICE_STATES, GUILD_VOICE_EVENTS, and potentially GUILD_MESSAGES).DiscordVoice class (provided by this package) to connect to a voice channel:
use Discord\Discord;
use Discord\Voice\DiscordVoice;
use Discord\Parts\Channel\Channel;
$discord = new Discord([...]);
$discord->on('ready', function (Discord $discord) {
// Get a text channel to confirm, and a voice channel to join
$textChannel = $discord->channels->get('id', '123...');
$voiceChannel = $discord->channels->get('id', '456...'); // Must be GUILD_VOICE
$voice = new DiscordVoice($discord);
$connection = yield $voice->join($voiceChannel);
// ... proceed to play audio
});
$discord->run();
ext-sodium, and FFmpeg installed on the system.Begin with the examples/ folder — especially voice-player.php, which demonstrates streaming local files and YouTube URLs via FFmpeg.
ffmpeg subprocesses (FFMpegStream), not raw buffers. Use:
$stream = new FFMpegStream('https://example.com/audio.mp3');
$connection->play($stream);
AudioProvider interface for custom sources (e.g., in-memory MP3, live mic via ffmpeg -f alsa), then pass to $connection->play($provider).$connection->on('end', function () {
echo "Playback finished.\n";
});
$connection->on('error', function (\Throwable $e) {
error_log("Voice error: {$e->getMessage()}");
});
DiscordVoice instances per guild — voice connections are not shared across guilds or channels.RawAudioProvider with manual Opus encoding (if needed) for low-latency features like voice modulators or filters. Leverage OpusEncoder class from Sop\Audio\Opus.Integration Tip: Wrap voice logic in a service class (e.g., VoiceManager) to reuse logic across commands or events. Inject Discord and cache voice connections per guild ID.
ffmpeg is in PATH. Often, explicit args help: use FFMpegStream::fromFile('file.mp3', ['-re']) for reliable streaming.await $connection->stop() on shutdown. Add gc_collect_cycles() after large playback sessions.ext-sodium is missing (not auto-confirmed by Composer). Verify with extension_loaded('sodium').DiscordVoice checks this and throws \RuntimeException.ready event, TLS handshake may fail. Fix: set openssl.cafile in php.ini to cacert.pem.sleep() inside on('ready')).$stream->setFfmpegLogLevel('info') to troubleshoot format/codec issues.OpusFfi decoding is used by default — disable via DiscordVoice::$preferOpusFfi = false if debugging packet loss. Prefer ext-libsodium for best performance.How can I help you explore Laravel packages today?