blob: 7c14ebc38eb97059017f87b88fca1584081a614e (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
|
package soundchan;
import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
import com.sedmelluq.discord.lavaplayer.player.event.AudioEventAdapter;
import com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioTrack;
import com.sedmelluq.discord.lavaplayer.track.AudioTrack;
import com.sedmelluq.discord.lavaplayer.track.AudioTrackEndReason;
import com.sedmelluq.discord.lavaplayer.track.DelegatedAudioTrack;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.BlockingDeque;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingDeque;
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 BlockingDeque<AudioTrack> queue;
/**
* @param player The audio player this scheduler uses
*/
public TrackScheduler(AudioPlayer player) {
this.player = player;
this.queue = new LinkedBlockingDeque<>();
}
/**
* 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);
}
}
public void playNow(AudioTrack track) {
AudioTrack currenlyPlaying = player.getPlayingTrack();
// If something is currently playing, pause it and put it back in the queue
if(currenlyPlaying != null) {
AudioTrack cloned = currenlyPlaying.makeClone();
cloned.setPosition(currenlyPlaying.getPosition());
// Don't re-enqueue if its just a soundclip
if (!(currenlyPlaying.getInfo().uri.contains(".mp3") || currenlyPlaying.getInfo().uri.contains(".wav")))
// Re-enqueue the track
queue.addFirst(cloned);
}
player.startTrack(track, false);
}
public List<String> getQueueContents() {
// Returns a list of the tracks in the queue
Object[] queueInfo = queue.toArray();
List<String> tracks = new ArrayList<>();
for (Object item:
queueInfo) {
tracks.add(((DelegatedAudioTrack) item).getInfo().title);
}
return tracks;
}
/**
* 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);
}
/**
* Cleans out the queue of tracks and stops any playing track
*/
public void emptyQueue() {
player.stopTrack();
queue.clear();
}
@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();
}
}
}
|