aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/soundbot/TrackScheduler.java
diff options
context:
space:
mode:
authorBrandon <bwaggone@umich.edu>2018-04-07 15:55:32 -0400
committerBrandon <bwaggone@umich.edu>2018-04-07 15:55:32 -0400
commit3799c7828ad8ef2968f39e4044db36885aec1ab4 (patch)
treea7da8fa945a22f62fc59ef983abced1ce082bb0c /src/main/java/soundbot/TrackScheduler.java
downloadSoundChan-3799c7828ad8ef2968f39e4044db36885aec1ab4.tar.gz
SoundChan-3799c7828ad8ef2968f39e4044db36885aec1ab4.tar.bz2
SoundChan-3799c7828ad8ef2968f39e4044db36885aec1ab4.zip
Initial commit
Diffstat (limited to 'src/main/java/soundbot/TrackScheduler.java')
-rw-r--r--src/main/java/soundbot/TrackScheduler.java56
1 files changed, 56 insertions, 0 deletions
diff --git a/src/main/java/soundbot/TrackScheduler.java b/src/main/java/soundbot/TrackScheduler.java
new file mode 100644
index 0000000..4cbda71
--- /dev/null
+++ b/src/main/java/soundbot/TrackScheduler.java
@@ -0,0 +1,56 @@
+package soundbot;
+
+import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
+import com.sedmelluq.discord.lavaplayer.player.event.AudioEventAdapter;
+import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
+import com.sedmelluq.discord.lavaplayer.track.AudioTrackEndReason;
+
+import java.util.concurrent.BlockingQueue;
+import java.util.concurrent.LinkedBlockingQueue;
+
+/**
+ * This class schedules tracks for the audio player. It contains the queue of tracks.
+ */
+public class TrackScheduler extends AudioEventAdapter {
+ private final AudioPlayer player;
+ private final BlockingQueue<AudioTrack> queue;
+
+ /**
+ * @param player The audio player this scheduler uses
+ */
+ public TrackScheduler(AudioPlayer player) {
+ this.player = player;
+ this.queue = new LinkedBlockingQueue<>();
+ }
+
+ /**
+ * Add the next track to queue or play right away if nothing is in the queue.
+ *
+ * @param track The track to play or add to queue.
+ */
+ public void queue(AudioTrack track) {
+ // Calling startTrack with the noInterrupt set to true will start the track only if nothing is currently playing. If
+ // something is playing, it returns false and does nothing. In that case the player was already playing so this
+ // track goes to the queue instead.
+ if (!player.startTrack(track, true)) {
+ queue.offer(track);
+ }
+ }
+
+ /**
+ * Start the next track, stopping the current one if it is playing.
+ */
+ public void nextTrack() {
+ // Start the next track, regardless of if something is already playing or not. In case queue was empty, we are
+ // giving null to startTrack, which is a valid argument and will simply stop the player.
+ player.startTrack(queue.poll(), false);
+ }
+
+ @Override
+ public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason endReason) {
+ // Only start the next track if the end reason is suitable for it (FINISHED or LOAD_FAILED)
+ if (endReason.mayStartNext) {
+ nextTrack();
+ }
+ }
+}