Search in sources :

Example 36 with AudioTrack

use of com.sedmelluq.discord.lavaplayer.track.AudioTrack in project lavaplayer by sedmelluq.

the class MusicController method addTrack.

private void addTrack(final Message message, final String identifier, final boolean now) {
    outputChannel.set((TextChannel) message.getChannel());
    manager.loadItemOrdered(this, identifier, new AudioLoadResultHandler() {

        @Override
        public void trackLoaded(AudioTrack track) {
            connectToFirstVoiceChannel(guild.getAudioManager());
            message.getChannel().sendMessage("Starting now: " + track.getInfo().title + " (length " + track.getDuration() + ")").queue();
            if (now) {
                scheduler.playNow(track, true);
            } else {
                scheduler.addToQueue(track);
            }
        }

        @Override
        public void playlistLoaded(AudioPlaylist playlist) {
            List<AudioTrack> tracks = playlist.getTracks();
            message.getChannel().sendMessage("Loaded playlist: " + playlist.getName() + " (" + tracks.size() + ")").queue();
            connectToFirstVoiceChannel(guild.getAudioManager());
            AudioTrack selected = playlist.getSelectedTrack();
            if (selected != null) {
                message.getChannel().sendMessage("Selected track from playlist: " + selected.getInfo().title).queue();
            } else {
                selected = tracks.get(0);
                message.getChannel().sendMessage("Added first track from playlist: " + selected.getInfo().title).queue();
            }
            if (now) {
                scheduler.playNow(selected, true);
            } else {
                scheduler.addToQueue(selected);
            }
            for (int i = 0; i < Math.min(10, playlist.getTracks().size()); i++) {
                if (tracks.get(i) != selected) {
                    scheduler.addToQueue(tracks.get(i));
                }
            }
        }

        @Override
        public void noMatches() {
            message.getChannel().sendMessage("Nothing found for " + identifier).queue();
        }

        @Override
        public void loadFailed(FriendlyException throwable) {
            message.getChannel().sendMessage("Failed with message: " + throwable.getMessage() + " (" + throwable.getClass().getSimpleName() + ")").queue();
        }
    });
}
Also used : AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) List(java.util.List) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException)

Example 37 with AudioTrack

use of com.sedmelluq.discord.lavaplayer.track.AudioTrack in project lavaplayer by sedmelluq.

the class MusicController method serialize.

@BotCommandHandler
private void serialize(Message message) throws IOException {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    MessageOutput outputStream = new MessageOutput(baos);
    for (AudioTrack track : scheduler.drainQueue()) {
        manager.encodeTrack(outputStream, track);
    }
    outputStream.finish();
    message.getChannel().sendMessage(Base64.encodeBytes(baos.toByteArray())).queue();
}
Also used : MessageOutput(com.sedmelluq.discord.lavaplayer.tools.io.MessageOutput) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BotCommandHandler(com.sedmelluq.discord.lavaplayer.demo.controller.BotCommandHandler)

Example 38 with AudioTrack

use of com.sedmelluq.discord.lavaplayer.track.AudioTrack in project lavaplayer by sedmelluq.

the class MusicController method buildReportForNode.

private String buildReportForNode(RemoteNode node) {
    StringBuilder builder = new StringBuilder();
    builder.append("--- ").append(node.getAddress()).append(" ---\n");
    builder.append("Connection state: ").append(node.getConnectionState()).append("\n");
    NodeStatisticsMessage statistics = node.getLastStatistics();
    builder.append("Node global statistics: \n").append(statistics == null ? "unavailable" : "");
    if (statistics != null) {
        builder.append("   playing tracks: ").append(statistics.playingTrackCount).append("\n");
        builder.append("   total tracks: ").append(statistics.totalTrackCount).append("\n");
        builder.append("   system CPU usage: ").append(statistics.systemCpuUsage).append("\n");
        builder.append("   process CPU usage: ").append(statistics.processCpuUsage).append("\n");
    }
    builder.append("Minimum tick interval: ").append(node.getTickMinimumInterval()).append("\n");
    builder.append("Tick history capacity: ").append(node.getTickHistoryCapacity()).append("\n");
    List<RemoteNode.Tick> ticks = node.getLastTicks(false);
    builder.append("Number of ticks in history: ").append(ticks.size()).append("\n");
    if (ticks.size() > 0) {
        int tail = Math.min(ticks.size(), 3);
        builder.append("Last ").append(tail).append(" ticks:\n");
        for (int i = ticks.size() - tail; i < ticks.size(); i++) {
            RemoteNode.Tick tick = ticks.get(i);
            builder.append("   [duration ").append(tick.endTime - tick.startTime).append("]\n");
            builder.append("   start time: ").append(tick.startTime).append("\n");
            builder.append("   end time: ").append(tick.endTime).append("\n");
            builder.append("   response code: ").append(tick.responseCode).append("\n");
            builder.append("   request size: ").append(tick.requestSize).append("\n");
            builder.append("   response size: ").append(tick.responseSize).append("\n");
        }
    }
    List<AudioTrack> tracks = node.getPlayingTracks();
    builder.append("Number of playing tracks: ").append(tracks.size()).append("\n");
    if (tracks.size() > 0) {
        int head = Math.min(tracks.size(), 3);
        builder.append("First ").append(head).append(" tracks:\n");
        for (int i = 0; i < head; i++) {
            AudioTrack track = tracks.get(i);
            builder.append("   [identifier ").append(track.getInfo().identifier).append("]\n");
            builder.append("   name: ").append(track.getInfo().author).append(" - ").append(track.getInfo().title).append("\n");
            builder.append("   progress: ").append(track.getPosition()).append(" / ").append(track.getDuration()).append("\n");
        }
    }
    builder.append("Balancer penalties: ").append(tracks.size()).append("\n");
    for (Map.Entry<String, Integer> penalty : node.getBalancerPenaltyDetails().entrySet()) {
        builder.append("   ").append(penalty.getKey()).append(": ").append(penalty.getValue()).append("\n");
    }
    return builder.toString();
}
Also used : RemoteNode(com.sedmelluq.discord.lavaplayer.remote.RemoteNode) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) NodeStatisticsMessage(com.sedmelluq.discord.lavaplayer.remote.message.NodeStatisticsMessage) Map(java.util.Map)

Example 39 with AudioTrack

use of com.sedmelluq.discord.lavaplayer.track.AudioTrack in project lavaplayer by sedmelluq.

the class PlayerManagerTestTools method loadTrack.

public static AudioTrack loadTrack(AudioPlayerManager manager, String identifier) throws Exception {
    CompletableFuture<AudioTrack> result = new CompletableFuture<>();
    manager.loadItem(identifier, new FunctionalResultHandler(result::complete, (playlist) -> result.completeExceptionally(new IllegalArgumentException()), () -> result.completeExceptionally(new NoSuchElementException()), result::completeExceptionally));
    return result.get(10, TimeUnit.SECONDS);
}
Also used : AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) AudioPlayerManager(com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager) TimeUnit(java.util.concurrent.TimeUnit) CRC32(java.util.zip.CRC32) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) CompletableFuture(java.util.concurrent.CompletableFuture) NoSuchElementException(java.util.NoSuchElementException) FunctionalResultHandler(com.sedmelluq.discord.lavaplayer.player.FunctionalResultHandler) MutableAudioFrame(com.sedmelluq.discord.lavaplayer.track.playback.MutableAudioFrame) ByteBuffer(java.nio.ByteBuffer) CompletableFuture(java.util.concurrent.CompletableFuture) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) FunctionalResultHandler(com.sedmelluq.discord.lavaplayer.player.FunctionalResultHandler) NoSuchElementException(java.util.NoSuchElementException)

Example 40 with AudioTrack

use of com.sedmelluq.discord.lavaplayer.track.AudioTrack in project Rubicon by Rubicon-Bot.

the class MusicManager method loadSong.

public void loadSong(boolean force) {
    TextChannel textChannel = parsedCommandInvocation.getMessage().getTextChannel();
    boolean isURL = false;
    StringBuilder searchParam = new StringBuilder();
    for (int i = 0; i < args.length; i++) searchParam.append(args[i]);
    if (searchParam.toString().startsWith("http://") || searchParam.toString().startsWith("https://"))
        isURL = true;
    if (!isURL)
        searchParam.insert(0, "ytsearch: ");
    final EmbedBuilder embedBuilder = new EmbedBuilder();
    final boolean isURLFinal = isURL;
    playerManager.loadItemOrdered(getCurrentMusicManager(), searchParam.toString(), new AudioLoadResultHandler() {

        @Override
        public void trackLoaded(AudioTrack audioTrack) {
            String trackName = audioTrack.getInfo().title;
            String trackAuthor = audioTrack.getInfo().author;
            String trackURL = audioTrack.getInfo().uri;
            boolean isStream = audioTrack.getInfo().isStream;
            long trackDuration = audioTrack.getDuration();
            if (!force) {
                getCurrentMusicManager().getScheduler().queue(audioTrack);
                embedBuilder.setAuthor("Added a new song to queue", trackURL, null);
                embedBuilder.addField("Title", trackName, true);
                embedBuilder.addField("Author", trackAuthor, true);
                embedBuilder.addField("Duration", (isStream) ? "Stream" : getTimestamp(trackDuration), false);
                embedBuilder.setColor(Colors.COLOR_PRIMARY);
                textChannel.sendMessage(embedBuilder.build()).queue();
            }
        }

        @Override
        public void playlistLoaded(AudioPlaylist audioPlaylist) {
            AudioTrack firstTrack = audioPlaylist.getSelectedTrack();
            List<AudioTrack> playlistTracks = audioPlaylist.getTracks();
            playlistTracks = playlistTracks.stream().limit(QUEUE_MAXIMUM - getCurrentMusicManager().getScheduler().getQueue().size()).collect(Collectors.toList());
            if (firstTrack == null)
                firstTrack = playlistTracks.get(0);
            if (force) {
                getCurrentMusicManager().getPlayer().playTrack(firstTrack);
                return;
            }
            if (isURLFinal) {
                playlistTracks.forEach(getCurrentMusicManager().getScheduler()::queue);
                embedBuilder.setTitle("Added playlist to queue");
                embedBuilder.setDescription("Added `" + playlistTracks.size() + "` songs from `" + audioPlaylist.getName() + "` to queue.\n" + "\n" + "**Now playing** `" + firstTrack.getInfo().title + "`");
                embedBuilder.addField("Author", firstTrack.getInfo().author, true);
                embedBuilder.addField("Duration", (firstTrack.getInfo().isStream) ? "Stream" : getTimestamp(firstTrack.getDuration()), false);
                textChannel.sendMessage(embedBuilder.build()).queue();
                embedBuilder.setColor(Colors.COLOR_PRIMARY);
                textChannel.sendMessage(embedBuilder.build()).queue();
            } else {
                MusicSearchResult musicSearchResult = new MusicSearchResult(parsedCommandInvocation.getAuthor(), guild, getCurrentMusicManager());
                audioPlaylist.getTracks().stream().limit(5).collect(Collectors.toList()).forEach(track -> {
                    try {
                        musicSearchResult.addTrack(track);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                });
                musicSearchResult.setMessage(textChannel.sendMessage(musicSearchResult.generateEmbed().build()).complete());
                musicChoose.add(musicSearchResult);
                new Timer().schedule(new TimerTask() {

                    @Override
                    public void run() {
                        if (musicChoose.contains(musicSearchResult)) {
                            musicSearchResult.getMessage().delete().queue();
                            musicChoose.remove(musicSearchResult);
                        }
                    }
                }, 15000);
            }
        }

        @Override
        public void noMatches() {
            embedBuilder.setTitle("No matches!");
            embedBuilder.setDescription("There are no matches.");
            embedBuilder.setColor(Colors.COLOR_NOT_IMPLEMENTED);
            textChannel.sendMessage(embedBuilder.build()).queue();
        }

        @Override
        public void loadFailed(FriendlyException e) {
            embedBuilder.setTitle(":warning: Error!");
            embedBuilder.setDescription("Could not play this song: " + e.getMessage());
            embedBuilder.setColor(Colors.COLOR_ERROR);
            textChannel.sendMessage(embedBuilder.build()).queue();
        }
    });
}
Also used : AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) AudioPlayer(com.sedmelluq.discord.lavaplayer.player.AudioPlayer) EmbedUtil(fun.rubicon.util.EmbedUtil) java.util(java.util) CommandManager(fun.rubicon.command.CommandManager) Lyrics(org.jmusixmatch.entity.lyrics.Lyrics) GuildMusicSQL(fun.rubicon.sql.GuildMusicSQL) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) MessageBuilder(net.dv8tion.jda.core.MessageBuilder) Permission(net.dv8tion.jda.core.Permission) HttpAudioSourceManager(com.sedmelluq.discord.lavaplayer.source.http.HttpAudioSourceManager) YoutubeAudioSourceManager(com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioSourceManager) AudioTrackInfo(com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) MusixMatch(org.jmusixmatch.MusixMatch) RubiconBot(fun.rubicon.RubiconBot) DefaultAudioPlayerManager(com.sedmelluq.discord.lavaplayer.player.DefaultAudioPlayerManager) MusixMatchException(org.jmusixmatch.MusixMatchException) Track(org.jmusixmatch.entity.track.Track) net.dv8tion.jda.core.entities(net.dv8tion.jda.core.entities) fun.rubicon.util(fun.rubicon.util) Collectors(java.util.stream.Collectors) AudioLoadResultHandler(com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) AudioPlayerManager(com.sedmelluq.discord.lavaplayer.player.AudioPlayerManager) UserMusicSQL(fun.rubicon.sql.UserMusicSQL) MessageReceivedEvent(net.dv8tion.jda.core.events.message.MessageReceivedEvent) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) PermissionException(net.dv8tion.jda.core.exceptions.PermissionException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) MusixMatchException(org.jmusixmatch.MusixMatchException) FriendlyException(com.sedmelluq.discord.lavaplayer.tools.FriendlyException) EmbedBuilder(net.dv8tion.jda.core.EmbedBuilder) AudioTrack(com.sedmelluq.discord.lavaplayer.track.AudioTrack) AudioPlaylist(com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)

Aggregations

AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)85 FriendlyException (com.sedmelluq.discord.lavaplayer.tools.FriendlyException)26 ArrayList (java.util.ArrayList)23 AudioPlaylist (com.sedmelluq.discord.lavaplayer.track.AudioPlaylist)21 AudioLoadResultHandler (com.sedmelluq.discord.lavaplayer.player.AudioLoadResultHandler)18 EmbedBuilder (net.dv8tion.jda.core.EmbedBuilder)17 AudioTrackInfo (com.sedmelluq.discord.lavaplayer.track.AudioTrackInfo)11 List (java.util.List)10 JsonBrowser (com.sedmelluq.discord.lavaplayer.tools.JsonBrowser)9 IOException (java.io.IOException)9 BasicAudioPlaylist (com.sedmelluq.discord.lavaplayer.track.BasicAudioPlaylist)8 TimeUnit (java.util.concurrent.TimeUnit)7 GuildMessageReceivedEvent (net.dv8tion.jda.core.events.message.guild.GuildMessageReceivedEvent)7 YoutubeAudioSourceManager (com.sedmelluq.discord.lavaplayer.source.youtube.YoutubeAudioSourceManager)6 Optional (java.util.Optional)6 MessageBuilder (net.dv8tion.jda.core.MessageBuilder)6 Permission (net.dv8tion.jda.core.Permission)6 AudioPlayer (com.sedmelluq.discord.lavaplayer.player.AudioPlayer)5 InternalAudioTrack (com.sedmelluq.discord.lavaplayer.track.InternalAudioTrack)5 Slf4j (lombok.extern.slf4j.Slf4j)5