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();
}
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);
}
}
}
Aggregations