AudioSendHandler interface is used to provide audio data to JDA for transmission to Discord voice channels.
Audio transmission requires the DAVE (Discord Audio Video Encryption) protocol. See the Discord documentation for details.
Audio Format
JDA expects audio in the following format (unless using pre-encoded Opus):- Sample Rate: 48 KHz
- Bit Depth: 16-bit
- Channels: Stereo (2 channels)
- Encoding: Signed BigEndian PCM
Methods
canProvide
Indicates whether audio data is available to send.true if audio data is ready to be provided- JDA calls this method before attempting to retrieve audio data
- Return
trueto indicate audio is ready,falseotherwise - This is checked each time JDA attempts to send audio
- Can be used to dynamically start/stop audio transmission
provide20MsAudio
Provides 20 milliseconds of audio data.A ByteBuffer containing 20ms of audio data, or
null if no data available- Must return an array-backed ByteBuffer
- Use
ByteBuffer.allocate(int)orByteBuffer.wrap(byte[]) - Must contain exactly 20 milliseconds of audio
- Audio format: 48KHz 16-bit stereo signed BigEndian PCM (unless
isOpus()returnstrue)
isOpus
Indicates whether the audio data is pre-encoded in Opus format.true if providing pre-encoded Opus audio, false for PCM (default)- Return
trueifprovide20MsAudio()returns pre-encoded Opus packets - Return
false(default) if providing raw PCM audio - When
true, JDA will not encode the audio and will send it as-is to Discord
Implementation Example
Here’s a complete example of an AudioSendHandler:Using with AudioManager
Recommended Implementation
JDA recommends using LavaPlayer as your AudioSendHandler implementation. It provides a complete audio playback system with support for various audio sources.See the LavaPlayer JDA demo for integration examples.
Audio Data Size
For 20ms of PCM audio at 48KHz 16-bit stereo:- Sample rate: 48,000 samples per second
- Duration: 20ms = 0.02 seconds
- Samples for 20ms: 48,000 × 0.02 = 960 samples
- Channels: 2 (stereo)
- Bytes per sample: 2 (16-bit)
- Total bytes: 960 × 2 × 2 = 3,840 bytes