Fix the echo command

This commit is contained in:
GregTCLTK 2020-02-20 20:15:46 +01:00
parent 569852042c
commit c53e8164f1
No known key found for this signature in database
GPG key ID: A91BADE5C070FF67

View file

@ -20,14 +20,18 @@ import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
import com.sedmelluq.discord.lavaplayer.track.playback.AudioFrame; import com.sedmelluq.discord.lavaplayer.track.playback.AudioFrame;
import net.dv8tion.jda.api.audio.AudioReceiveHandler; import net.dv8tion.jda.api.audio.AudioReceiveHandler;
import net.dv8tion.jda.api.audio.AudioSendHandler; import net.dv8tion.jda.api.audio.AudioSendHandler;
import net.dv8tion.jda.api.audio.CombinedAudio;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;
public class AudioPlayerSendHandler implements AudioSendHandler, AudioReceiveHandler { public class AudioPlayerSendHandler implements AudioSendHandler, AudioReceiveHandler {
private final AudioPlayer audioPlayer; private final AudioPlayer audioPlayer;
private AudioFrame lastFrame; private AudioFrame lastFrame;
private final Queue<byte[]> queue = new ConcurrentLinkedQueue<>();
public AudioPlayerSendHandler(AudioPlayer audioPlayer) { public AudioPlayerSendHandler(AudioPlayer audioPlayer) {
this.audioPlayer = audioPlayer; this.audioPlayer = audioPlayer;
@ -35,16 +39,22 @@ public class AudioPlayerSendHandler implements AudioSendHandler, AudioReceiveHan
@Override @Override
public boolean canProvide() { public boolean canProvide() {
if (lastFrame == null) { if (audioPlayer.getPlayingTrack() == null)
return !queue.isEmpty();
else if (lastFrame == null) {
lastFrame = audioPlayer.provide(); lastFrame = audioPlayer.provide();
}
return lastFrame != null; return lastFrame != null;
} }
return false;
}
@Nullable @Nullable
@Override @Override
public ByteBuffer provide20MsAudio() { public ByteBuffer provide20MsAudio() {
if (audioPlayer.getPlayingTrack() == null) {
byte[] data = queue.poll();
return data == null ? null : ByteBuffer.wrap(data);
} else {
if (lastFrame == null) { if (lastFrame == null) {
lastFrame = audioPlayer.provide(); lastFrame = audioPlayer.provide();
} }
@ -54,9 +64,24 @@ public class AudioPlayerSendHandler implements AudioSendHandler, AudioReceiveHan
return ByteBuffer.wrap(data); return ByteBuffer.wrap(data);
} }
}
@Override
public boolean canReceiveCombined() {
return queue.size() < 10;
}
@Override
public void handleCombinedAudio(CombinedAudio combinedAudio) {
if (combinedAudio.getUsers().isEmpty())
return;
byte[] data = combinedAudio.getAudioData(1.0f);
queue.add(data);
}
@Override @Override
public boolean isOpus() { public boolean isOpus() {
return true; return audioPlayer.getPlayingTrack() != null;
} }
} }