ClaptrapBot/src/main/java/net/Broken/audio/TrackScheduler.java

143 lines
4.7 KiB
Java
Raw Normal View History

2017-10-22 23:48:13 +02:00
package net.Broken.audio;
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 com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo;
import net.Broken.RestApi.Data.UserAudioTrackData;
2018-01-13 19:12:02 +01:00
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
2017-10-22 23:48:13 +02:00
import java.util.ArrayList;
import java.util.List;
2017-11-04 18:27:18 +01:00
import java.util.concurrent.BlockingDeque;
2017-10-22 23:48:13 +02:00
import java.util.concurrent.BlockingQueue;
2017-11-04 18:27:18 +01:00
import java.util.concurrent.LinkedBlockingDeque;
2017-10-22 23:48:13 +02:00
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<UserAudioTrack> queue;
private UserAudioTrack currentPlayingTrack;
2018-01-13 19:12:02 +01:00
Logger logger = LogManager.getLogger();
2017-10-22 23:48:13 +02:00
/**
* @param player The audio player this scheduler uses
*/
public TrackScheduler(AudioPlayer player) {
this.player = player;
player.setVolume(25);
2017-11-04 18:27:18 +01:00
this.queue = new LinkedBlockingDeque<>();
this.currentPlayingTrack = null;
2017-10-22 23:48:13 +02:00
}
/**
* 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(UserAudioTrack track) {
2017-10-22 23:48:13 +02:00
// 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.getAudioTrack(), true)) {
2017-10-22 23:48:13 +02:00
queue.offer(track);
}
else{
currentPlayingTrack = track;
}
2017-10-22 23:48:13 +02:00
}
public void addNext(UserAudioTrack track) {
2017-11-04 18:27:18 +01:00
// 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.getAudioTrack(), true)) {
2017-11-04 18:27:18 +01:00
queue.addFirst(track);
}
else{
currentPlayingTrack = track;
}
2017-11-04 18:27:18 +01:00
}
2017-10-22 23:48:13 +02:00
public void pause() {
player.setPaused(true);
}
public void resume() {
player.setPaused(false);
}
public void stop(){
player.stopTrack();
this.currentPlayingTrack = null;
2017-10-22 23:48:13 +02:00
player.destroy();
}
public void flush(){
queue.clear();
}
public List<UserAudioTrackData> getList(){
2017-10-22 23:48:13 +02:00
// AudioTrack[] test = (AudioTrack[]) queue.toArray();
List<UserAudioTrackData> temp = new ArrayList<>();
2017-10-22 23:48:13 +02:00
Object[] test = queue.toArray();
for(Object track: test){
UserAudioTrack casted = (UserAudioTrack) track;
temp.add(new UserAudioTrackData(casted.getSubmitedUser().getName(), casted.getAudioTrack().getInfo()));
2017-10-22 23:48:13 +02:00
}
return temp;
}
public AudioTrackInfo getInfo(){
return player.getPlayingTrack().getInfo();
}
public UserAudioTrack getCurrentPlayingTrack() {
return currentPlayingTrack;
}
2018-01-13 19:12:02 +01:00
public boolean remove(String uri){
for(UserAudioTrack track : queue){
if(track.getAudioTrack().getInfo().uri.equals(uri)){
2018-01-13 19:12:02 +01:00
if(!queue.remove(track)) {
logger.info("Delete failure!");
return false;
} else {
logger.info("Delete succeful");
return true;
}
}
}
logger.info("Delete failure! Not found.");
return false;
}
2017-10-22 23:48:13 +02:00
/**
* 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.
UserAudioTrack track = queue.poll();
this.currentPlayingTrack = track;
if(track != null)
player.startTrack(track.getAudioTrack(), false);
2017-10-22 23:48:13 +02:00
}
@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();
}
}
}