package me.mrkirby153.KirBot.music

import com.sedmelluq.discord.lavaplayer.player.AudioPlayer
import com.sedmelluq.discord.lavaplayer.track.playback.AudioFrame
import net.dv8tion.jda.core.audio.AudioSendHandler

class AudioPlayerSendHandler(val audioPlayer: AudioPlayer): AudioSendHandler {

    private var lastFrame: AudioFrame? =  null

    override fun provide20MsAudio(): ByteArray = lastFrame!!.data;

    override fun canProvide(): Boolean {
        lastFrame = audioPlayer.provide()
        return lastFrame != null
    }

    override fun isOpus() = true
}
package me.mrkirby153.KirBot.music

import com.sedmelluq.discord.lavaplayer.player.AudioPlayer
import com.sedmelluq.discord.lavaplayer.track.playback.AudioFrame
import net.dv8tion.jda.core.audio.AudioSendHandler

class AudioPlayerSendHandler(val audioPlayer: AudioPlayer) : AudioSendHandler {

    private var lastFrame: AudioFrame? = null

    override fun provide20MsAudio(): ByteArray? {
        if (lastFrame == null)
            lastFrame = audioPlayer.provide()
        val data = lastFrame?.data
        lastFrame = null
        return data
    }

    override fun canProvide(): Boolean {
        lastFrame = audioPlayer.provide()
        return lastFrame != null
    }

    override fun isOpus() = true
}
Fix race condition that would cause streams to fail sometimes
Fix race condition that would cause streams to fail sometimes

mrkirby153/KirBot