Search in sources :

Example 1 with MessageOutput

use of com.sedmelluq.discord.lavaplayer.tools.io.MessageOutput 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 2 with MessageOutput

use of com.sedmelluq.discord.lavaplayer.tools.io.MessageOutput in project FredBoat by Frederikam.

the class MusicPersistenceHandler method persist.

private void persist(int code) {
    File dir = new File("music_persistence");
    if (!dir.exists()) {
        boolean created = dir.mkdir();
        if (!created) {
            log.error("Failed to create music persistence directory");
            return;
        }
    }
    Map<Long, GuildPlayer> reg = playerRegistry.getRegistry();
    boolean isUpdate = code == ExitCodes.EXIT_CODE_UPDATE;
    boolean isRestart = code == ExitCodes.EXIT_CODE_RESTART;
    for (long gId : reg.keySet()) {
        try {
            GuildPlayer player = reg.get(gId);
            String msg;
            if (isUpdate) {
                msg = I18n.get(player.getGuild()).getString("shutdownUpdating");
            } else if (isRestart) {
                msg = I18n.get(player.getGuild()).getString("shutdownRestarting");
            } else {
                msg = I18n.get(player.getGuild()).getString("shutdownIndef");
            }
            TextChannel activeTextChannel = player.getActiveTextChannel();
            List<CompletableFuture> announcements = new ArrayList<>();
            if (activeTextChannel != null && player.isPlaying()) {
                announcements.add(CentralMessaging.message(activeTextChannel, msg).send(null));
            }
            for (Future announcement : announcements) {
                try {
                    // 30 seconds is enough on patron boat
                    announcement.get(30, TimeUnit.SECONDS);
                } catch (Exception ignored) {
                }
            }
            JSONObject data = new JSONObject();
            VoiceChannel vc = player.getCurrentVoiceChannel();
            data.put("vc", vc != null ? vc.getId() : "0");
            data.put("tc", activeTextChannel != null ? activeTextChannel.getId() : "");
            data.put("isPaused", player.isPaused());
            data.put("volume", Float.toString(player.getVolume()));
            data.put("repeatMode", player.getRepeatMode());
            data.put("shuffle", player.isShuffle());
            if (player.getPlayingTrack() != null) {
                data.put("position", player.getPosition());
            }
            ArrayList<JSONObject> identifiers = new ArrayList<>();
            for (AudioTrackContext atc : player.getRemainingTracks()) {
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                audioPlayerManager.encodeTrack(new MessageOutput(baos), atc.getTrack());
                JSONObject ident = new JSONObject().put("message", Base64.encodeBase64String(baos.toByteArray())).put("user", atc.getUserId());
                if (atc instanceof SplitAudioTrackContext) {
                    JSONObject split = new JSONObject();
                    SplitAudioTrackContext c = (SplitAudioTrackContext) atc;
                    split.put("title", c.getEffectiveTitle()).put("startPos", c.getStartPosition()).put("endPos", c.getStartPosition() + c.getEffectiveDuration());
                    ident.put("split", split);
                }
                identifiers.add(ident);
            }
            data.put("sources", identifiers);
            try {
                FileUtils.writeStringToFile(new File(dir, Long.toString(gId)), data.toString(), Charset.forName("UTF-8"));
            } catch (IOException ex) {
                if (activeTextChannel != null) {
                    CentralMessaging.message(activeTextChannel, MessageFormat.format(I18n.get(player.getGuild()).getString("shutdownPersistenceFail"), ex.getMessage())).send(null);
                }
            }
        } catch (Exception ex) {
            log.error("Error when saving persistence file", ex);
        }
    }
}
Also used : MessageOutput(com.sedmelluq.discord.lavaplayer.tools.io.MessageOutput) ArrayList(java.util.ArrayList) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) SplitAudioTrackContext(fredboat.audio.queue.SplitAudioTrackContext) IOException(java.io.IOException) TextChannel(net.dv8tion.jda.core.entities.TextChannel) CompletableFuture(java.util.concurrent.CompletableFuture) JSONObject(org.json.JSONObject) GuildPlayer(fredboat.audio.player.GuildPlayer) CompletableFuture(java.util.concurrent.CompletableFuture) Future(java.util.concurrent.Future) VoiceChannel(net.dv8tion.jda.core.entities.VoiceChannel) AudioTrackContext(fredboat.audio.queue.AudioTrackContext) SplitAudioTrackContext(fredboat.audio.queue.SplitAudioTrackContext) File(java.io.File)

Aggregations

MessageOutput (com.sedmelluq.discord.lavaplayer.tools.io.MessageOutput)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 BotCommandHandler (com.sedmelluq.discord.lavaplayer.demo.controller.BotCommandHandler)1 AudioTrack (com.sedmelluq.discord.lavaplayer.track.AudioTrack)1 GuildPlayer (fredboat.audio.player.GuildPlayer)1 AudioTrackContext (fredboat.audio.queue.AudioTrackContext)1 SplitAudioTrackContext (fredboat.audio.queue.SplitAudioTrackContext)1 File (java.io.File)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Future (java.util.concurrent.Future)1 TextChannel (net.dv8tion.jda.core.entities.TextChannel)1 VoiceChannel (net.dv8tion.jda.core.entities.VoiceChannel)1 JSONObject (org.json.JSONObject)1