From 7eeb97fc307257290acfbf6ecbb055cdc5788c2b Mon Sep 17 00:00:00 2001 From: Brandon Date: Sat, 7 Apr 2018 17:09:12 -0400 Subject: Rename main, add properties loading --- .../java/soundchan/AudioPlayerSendHandler.java | 48 ++++++++++++++++++++++ 1 file changed, 48 insertions(+) create mode 100644 src/main/java/soundchan/AudioPlayerSendHandler.java (limited to 'src/main/java/soundchan/AudioPlayerSendHandler.java') diff --git a/src/main/java/soundchan/AudioPlayerSendHandler.java b/src/main/java/soundchan/AudioPlayerSendHandler.java new file mode 100644 index 0000000..cd76807 --- /dev/null +++ b/src/main/java/soundchan/AudioPlayerSendHandler.java @@ -0,0 +1,48 @@ +package soundchan; + +import com.sedmelluq.discord.lavaplayer.player.AudioPlayer; +import com.sedmelluq.discord.lavaplayer.track.playback.AudioFrame; +import net.dv8tion.jda.core.audio.AudioSendHandler; + +/** + * This is a wrapper around AudioPlayer which makes it behave as an AudioSendHandler for JDA. As JDA calls canProvide + * before every call to provide20MsAudio(), we pull the frame in canProvide() and use the frame we already pulled in + * provide20MsAudio(). + */ +public class AudioPlayerSendHandler implements AudioSendHandler { + private final AudioPlayer audioPlayer; + private AudioFrame lastFrame; + + /** + * @param audioPlayer Audio player to wrap. + */ + public AudioPlayerSendHandler(AudioPlayer audioPlayer) { + this.audioPlayer = audioPlayer; + } + + @Override + public boolean canProvide() { + if (lastFrame == null) { + lastFrame = audioPlayer.provide(); + } + + return lastFrame != null; + } + + @Override + public byte[] provide20MsAudio() { + if (lastFrame == null) { + lastFrame = audioPlayer.provide(); + } + + byte[] data = lastFrame != null ? lastFrame.data : null; + lastFrame = null; + + return data; + } + + @Override + public boolean isOpus() { + return true; + } +} -- cgit v1.2.3