Exoplayer audio stream to Discord bot produces static

Help! My Discord bot is broadcasting noise instead of music

I’m trying to set up a Discord bot that streams audio from my Android device using Exoplayer. The bot connects to the voice channel fine, but all it broadcasts is static. Here’s what I’ve done so far:

  • Set up Exoplayer to play a local MP3 file (works on the phone)
  • Used Kord library for the Discord bot
  • Configured an Opus codec with 48kHz stereo settings
  • Implemented an AudioProcessor to get PCM data
  • Encoded the PCM data to Opus format
  • Sent the encoded data to the Discord bot

I’m pretty sure the issue is either how I’m grabbing the audio data or how I’m passing it to the bot. Has anyone run into this before? Any ideas on what I might be doing wrong?

Here’s a simplified version of my code:

class BotAudioProcessor(private val listener: AudioDataListener) : BaseAudioProcessor() {
    private val codec = Opus()
    
    override fun queueInput(inputBuffer: ByteBuffer) {
        val frame = ShortArray(960)
        inputBuffer.asShortBuffer().get(frame)
        val encoded = codec.encode(frame, 960)
        listener.onAudioData(encoded!!.toByteArray())
    }
}

class MusicBot : AudioDataListener {
    private var audioBuffer: ByteArray? = null
    
    suspend fun start() {
        voiceChannel.connect {
            audioProvider {
                audioBuffer?.let { AudioFrame(it) }
            }
        }
    }
    
    override fun onAudioData(data: ByteArray) {
        audioBuffer = data
    }
}

Any help would be greatly appreciated!

I’ve dealt with similar audio streaming issues before. One thing that often gets overlooked is proper synchronization between the audio capture and encoding processes. Make sure you’re not dropping frames or introducing gaps in the audio stream.

Also, have you considered using a circular buffer to manage the audio data? It can help smooth out any irregularities in the timing of audio capture and transmission.

Another potential issue could be with the network. Discord’s voice servers can be picky about packet timing. Try implementing a jitter buffer on the bot side to handle any network inconsistencies.

Lastly, double-check your Opus encoder settings. Sometimes, the default settings aren’t optimal for voice chat applications. Experiment with different bitrates and frame sizes to find the sweet spot for audio quality and stability.

If all else fails, you might want to look into using a established library like JDA (Java Discord API) which has built-in audio support. It could save you a lot of headaches in the long run.

I’ve encountered this issue before when working with Discord bots. One crucial aspect you might want to double-check is the buffer size. Discord expects audio packets of a specific size, typically 960 or 1920 samples per frame. Ensure your AudioProcessor is configured to output the correct frame size.

Additionally, verify that your Opus encoder settings match Discord’s requirements exactly. They’re quite particular about the bitrate and application type. Try setting the bitrate to 64000 and using OPUS_APPLICATION_VOIP as the application type.

Lastly, consider implementing a simple audio passthrough test without any encoding to isolate whether the issue is with the audio capture or the encoding process. This can help narrow down where the problem originates.

hey there! had a similar issue. make sure ur exoplayer’s audio format matches discord’s requirements (48khz, stereo, 16-bit). also, check if ur encoding the pcm data correctly. might wanna try using lavaplayer instead of exoplayer - it handles discord audio stuff better. good luck!