Search code examples
discord-jdalavaplayer

How can I create a queue?


I'm trying to create a queue of tracks, but the problem is that all the information from the Internet is outdated, and I don't understand how to do it. I want my bot to play audio recordings in voice chat, and I can add audio recordings to the queue and have them play after the previous one ends, the only option that I found is through AudioTrack, but the problem is that I do not understand how to follow the link on a YouTube video, it should be redefined to the AudioTrack class, the only solution that I found was through loadItem, but it is also not correct due to the parameters in new versions.

private static AudioPlayerManager createAudioPlayerManager() {
    AudioPlayerManager manager = new DefaultAudioPlayerManager();
    manager.setFrameBufferDuration(5000);
    AudioSourceManagers.registerRemoteSources(manager);
    return manager;
}

public static AudioPlayer createAudioPlayer(VoiceChannel channel) {
    AudioManager audioManager = channel.getGuild().getAudioManager();
    AudioPlayer audioPlayer = createAudioPlayerManager().createPlayer();
    audioManager.setSendingHandler(new AudioPlayerSendHandler(audioPlayer));
    return audioPlayer;
}

public static AudioTrack loadPlaylist(String url, AudioPlayer player) throws ExecutionException, InterruptedException {
    AudioTrack track = createAudioPlayerManager().loadItem(url, new AudioLoader(player)).get();
    return track;
}

@Override
public void onTrackEnd(AudioPlayer player, AudioTrack track, AudioTrackEndReason endReason) {
    if (endReason.mayStartNext)
        playNextTrack(player);
}

public void addTrack(String videoUrl) {
    AudioTrack audio = loadPlaylist(url, this.player)
    if (this.player.getPlayingTrack() == null) {
        playNextTrack(this.player);
    }
}

Solution

  • The lavaplayer repository on GitHub provides a demo, which includes TrackScheduler to manage a queue of tracks.