lavalink.client.player.IPlayer Java Examples

The following examples show how to use lavalink.client.player.IPlayer. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: MusicCmds.java    From MantaroBot with GNU General Public License v3.0 6 votes vote down vote up
/**
 * This only fires on manual stop!
 *
 * @param ctx The command context
 */
private void stop(Context ctx) {
    try {
        GuildMusicManager musicManager = ctx.getAudioManager().getMusicManager(ctx.getGuild());
        TrackScheduler trackScheduler = musicManager.getTrackScheduler();
        IPlayer musicPlayer = trackScheduler.getMusicPlayer();

        if (musicPlayer.getPlayingTrack() != null && !musicPlayer.isPaused()) {
            musicPlayer.stopTrack();
        }

        int TEMP_QUEUE_LENGTH = trackScheduler.getQueue().size();
        trackScheduler.getQueue().clear();

        if (TEMP_QUEUE_LENGTH > 0) {
            ctx.sendLocalized("commands.stop.cleanup", EmoteReference.OK, TEMP_QUEUE_LENGTH);
        }

        //This ends up calling TrackScheduler#onTrackStart -> currentTrack == null -> TrackScheduler#onStop!
        //Beware to not close the connection twice...
        trackScheduler.nextTrack(true, true);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #2
Source File: PlayerListenerAdapter.java    From JuniperBot with GNU General Public License v3.0 6 votes vote down vote up
@Override
public void onEvent(PlayerEvent event) {
    IPlayer player = event.getPlayer();
    PlaybackInstance instance = instancesByPlayer.get(player);

    if (event instanceof lavalink.client.player.event.TrackStartEvent) {
        if (instance != null) {
            onTrackStart(instance);
        }
    } else if (event instanceof lavalink.client.player.event.TrackEndEvent) {
        if (instance != null) {
            onTrackEnd(instance, ((lavalink.client.player.event.TrackEndEvent) event).getReason());
        }
    } else if (event instanceof lavalink.client.player.event.TrackExceptionEvent) {
        Exception e = ((lavalink.client.player.event.TrackExceptionEvent) event).getException();
        FriendlyException fe = e instanceof FriendlyException
                ? (FriendlyException) e
                : new FriendlyException("Unexpected exception", FriendlyException.Severity.SUSPICIOUS, e);
        onTrackException(instance, fe);
    } else if (event instanceof lavalink.client.player.event.TrackStuckEvent) {
        onTrackStuck(instance);
    }
}
 
Example #3
Source File: TrackScheduler.java    From MantaroBot with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onTrackException(IPlayer player, AudioTrack track, Exception exception) {
    if (getRequestedTextChannel() != null && getRequestedTextChannel().canTalk()) {
        //Avoid massive spam of when song error in mass.
        if ((lastErrorSentAt == 0 || lastErrorSentAt + 60000 < System.currentTimeMillis()) && errorCount < 10) {
            lastErrorSentAt = System.currentTimeMillis();
            getRequestedTextChannel().sendMessageFormat(language.get("commands.music_general.track_error"), EmoteReference.SAD).queue();
        }

        errorCount++;
    }
}
 
Example #4
Source File: TrackScheduler.java    From MantaroBot with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onTrackEnd(IPlayer player, AudioTrack track, AudioTrackEndReason endReason) {
    if (endReason.mayStartNext) {
        nextTrack(false, false);
        onTrackStart();
    }
}
 
Example #5
Source File: DefaultAudioServiceImpl.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void openConnection(IPlayer player, VoiceChannel channel) {
    if (lavaLink != null) {
        lavaLink.getLink(channel.getGuild()).connect(channel);
    } else {
        AudioManager audioManager = channel.getGuild().getAudioManager();
        if (audioManager.getConnectedChannel() == null) {
            audioManager.setSendingHandler(new GuildAudioSendHandler(player));
        }
        channel.getGuild().getAudioManager().openAudioConnection(channel);
    }
}
 
Example #6
Source File: PlayerServiceImpl.java    From JuniperBot with GNU General Public License v3.0 5 votes vote down vote up
@Override
@Transactional
public PlaybackInstance get(long guildId, boolean create) {
    if (!create) {
        return instances.get(guildId);
    }
    return instances.computeIfAbsent(guildId, e -> {
        MusicConfig config = musicConfigService.getOrCreate(guildId);
        IPlayer player = lavaAudioService.createPlayer(String.valueOf(guildId));
        if (featureSetService.isAvailable(guildId)) {
            player.setVolume(config.getVoiceVolume());
        }
        return registerInstance(new PlaybackInstance(e, player));
    });
}
 
Example #7
Source File: TrackScheduler.java    From SkyBot with GNU Affero General Public License v3.0 5 votes vote down vote up
TrackScheduler(IPlayer player, GuildMusicManager guildMusicManager) {
    this.player = player;
    this.queue = new LinkedList<>();
    this.guildMusicManager = guildMusicManager;
    this.messageDebouncer = new Debouncer<>((msg) ->
        sendMsg(guildMusicManager.getLatestChannel(), msg, null, (t) -> {})
        , DEBOUNCE_INTERVAL);
}
 
Example #8
Source File: LavalinkManager.java    From SkyBot with GNU Affero General Public License v3.0 4 votes vote down vote up
public IPlayer createPlayer(long guildId) {
    return isEnabled()
        ? lavalink.getLink(String.valueOf(guildId)).getPlayer()
        : new LavaplayerPlayerWrapper(audioUtils.getPlayerManager().createPlayer());
}
 
Example #9
Source File: MusicCmds.java    From MantaroBot with GNU General Public License v3.0 4 votes vote down vote up
@Subscribe
public void np(CommandRegistry cr) {
    cr.register("np", new SimpleCommand(CommandCategory.MUSIC) {
        @Override
        public void call(Context ctx, String content, String[] args) {
            final GuildMusicManager musicManager = ctx.getAudioManager().getMusicManager(ctx.getGuild());
            final TrackScheduler trackScheduler = musicManager.getTrackScheduler();
            final IPlayer audioPlayer = trackScheduler.getMusicPlayer();

            if (audioPlayer == null || audioPlayer.getPlayingTrack() == null) {
                ctx.sendLocalized("commands.np.no_track", EmoteReference.ERROR);
                return;
            }

            EmbedBuilder npEmbed = new EmbedBuilder();
            long now = audioPlayer.getTrackPosition();
            long total = audioPlayer.getPlayingTrack().getDuration();
            I18nContext languageContext = ctx.getLanguageContext();

            npEmbed.setAuthor(languageContext.get("commands.np.header"), null, ctx.getGuild().getIconUrl())
                    .setThumbnail("http://www.clipartbest.com/cliparts/jix/6zx/jix6zx4dT.png")
                    .setDescription("\n\u23ef " + AudioCmdUtils.getProgressBar(now, total) + "\n\n" +
                            "**[" + musicManager.getTrackScheduler().getAudioPlayer().getPlayer().getPlayingTrack()
                            .getInfo().title + "]"
                            + "(" + musicManager.getTrackScheduler().getAudioPlayer().getPlayer().getPlayingTrack()
                            .getInfo().uri + ")** "
                            + String.format("`(%s/%s)`", Utils.getDurationMinutes(now), total == Long.MAX_VALUE ? "stream" : Utils.getDurationMinutes(total)))
                    .setFooter("Enjoy the music! <3. Use `~>lyrics current` to see the lyrics of the current song!", ctx.getAuthor().getAvatarUrl());

            ctx.send(npEmbed.build());
            TextChannelGround.of(ctx.getEvent()).dropItemWithChance(0, 10);
        }

        @Override
        public HelpContent help() {
            return new HelpContent.Builder()
                    .setDescription("See what track is playing now.")
                    .build();
        }
    });
}
 
Example #10
Source File: TrackScheduler.java    From MantaroBot with GNU General Public License v3.0 4 votes vote down vote up
public IPlayer getMusicPlayer() {
    return getAudioPlayer().getPlayer();
}
 
Example #11
Source File: DefaultAudioServiceImpl.java    From JuniperBot with GNU General Public License v3.0 4 votes vote down vote up
@Override
public IPlayer createPlayer(String guildId) {
    return lavaLink != null
            ? lavaLink.getLink(guildId).getPlayer()
            : new LavaplayerPlayerWrapper(playerManager.createPlayer());
}
 
Example #12
Source File: PlayerListenerAdapter.java    From JuniperBot with GNU General Public License v3.0 4 votes vote down vote up
protected void unregisterInstance(PlaybackInstance instance) {
    IPlayer player = instance.getPlayer();
    player.removeListener(this);
    instancesByPlayer.remove(player);
}
 
Example #13
Source File: PlayerListenerAdapter.java    From JuniperBot with GNU General Public License v3.0 4 votes vote down vote up
protected PlaybackInstance registerInstance(PlaybackInstance instance) {
    IPlayer player = instance.getPlayer();
    player.addListener(this);
    instancesByPlayer.put(player, instance);
    return instance;
}
 
Example #14
Source File: AudioMessageManager.java    From JuniperBot with GNU General Public License v3.0 4 votes vote down vote up
private EmbedBuilder getPlayMessage(TrackRequest request) {
    EmbedBuilder builder = getBasicMessage(request);
    builder.setDescription(null);

    AudioTrackInfo info = request.getTrack().getInfo();
    PlaybackInstance instance = TrackData.get(request.getTrack()).getInstance();
    boolean refreshable = isRefreshable(instance.getGuildId());

    String durationText;
    if (request.getEndReason() != null) {
        StringBuilder reasonBuilder = new StringBuilder();

        boolean hasDuration = !info.isStream && info.length > 0;
        if (hasDuration) {
            reasonBuilder.append(CommonUtils.formatDuration(request.getTrack().getDuration())).append(" (");
        }
        reasonBuilder.append(messageService.getEnumTitle(request.getEndReason()));

        String endMember = getMemberName(request, true);
        if (StringUtils.isNotBlank(endMember)) {
            reasonBuilder
                    .append(" - **")
                    .append(endMember)
                    .append("**");
        }
        if (hasDuration) {
            reasonBuilder.append(")");
        }
        reasonBuilder.append(CommonUtils.EMPTY_SYMBOL);
        durationText = reasonBuilder.toString();
    } else {
        durationText = getTextProgress(instance, request.getTrack(), refreshable);
    }

    if (instance.getPlaylistUuid() != null) {
        builder.setDescription(messageService.getMessage("discord.command.audio.panel.playlist",
                commonProperties.getBranding().getWebsiteUrl(), instance.getPlaylistUuid()));
    }

    int size = instance.getQueue().size();
    if (request.getEndReason() == null && size > 1) {
        MusicConfig config = musicConfigService.getByGuildId(instance.getGuildId());
        if (config != null && config.isShowQueue()) {
            List<TrackRequest> next = instance.getQueue().subList(1, Math.min(size, MAX_SHORT_QUEUE + 1));
            addQueue(builder, instance, next, 2 + instance.getCursor(), true);
        }
    }

    if (request.getTrack().getInfo().isStream || !refreshable) {
        builder.addField(messageService.getMessage("discord.command.audio.panel.duration"),
                durationText, true);
        builder.addField(messageService.getMessage("discord.command.audio.panel.requestedBy"),
                getMemberName(request, false), true);
    } else {
        String requestedBy = String.format("%s: %s",
                messageService.getMessage("discord.command.audio.panel.requestedBy"),
                getMemberName(request, false));
        builder.addField(requestedBy, durationText, true);
    }

    if (request.getEndReason() == null) {
        IPlayer player = instance.getPlayer();
        if (player.getVolume() != 100) {
            int volume = player.getVolume();
            builder.addField(messageService.getMessage("discord.command.audio.panel.volume"),
                    String.format("%d%% %s", volume, CommonUtils.getVolumeIcon(volume)), true);
        }
        if (!RepeatMode.NONE.equals(instance.getMode())) {
            builder.addField(messageService.getMessage("discord.command.audio.panel.repeatMode"),
                    instance.getMode().getEmoji(), true);
        }
        if (player.isPaused()) {
            builder.addField(messageService.getMessage("discord.command.audio.panel.paused"),
                    "\u23F8", true);
        }

        if (player instanceof LavalinkPlayer) {
            LavalinkPlayer lavalinkPlayer = (LavalinkPlayer) player;
            Link link = lavalinkPlayer.getLink();
            if (link != null) {
                LavalinkSocket socket = link.getNode(false);
                if (socket != null) {
                    StringBuilder statsBuilder = new StringBuilder(messageService
                            .getMessage("discord.command.audio.panel.poweredBy", socket.getName()));
                    if (refreshable && socket.getStats() != null) {
                        long load = Math.round(socket.getStats().getSystemLoad() * 100);
                        if (load < 0) load = 0;
                        if (load > 100) load = 100;
                        statsBuilder
                                .append(" ")
                                .append(messageService.getMessage("discord.command.audio.panel.load", load));
                    }
                    builder.setFooter(statsBuilder.toString(), null);
                }
            }
        }
    }
    return builder;
}
 
Example #15
Source File: PlaybackInstance.java    From JuniperBot with GNU General Public License v3.0 4 votes vote down vote up
public PlaybackInstance(long guildId, IPlayer player) {
    this.guildId = guildId;
    this.player = player;
    reset();
}
 
Example #16
Source File: GuildAudioSendHandler.java    From JuniperBot with GNU General Public License v3.0 4 votes vote down vote up
public GuildAudioSendHandler(IPlayer player) {
    this.player = (LavaplayerPlayerWrapper) player;
}
 
Example #17
Source File: AudioPlayerSenderHandler.java    From SkyBot with GNU Affero General Public License v3.0 4 votes vote down vote up
AudioPlayerSenderHandler(IPlayer audioPlayer) {
    this.audioPlayer = (LavaplayerPlayerWrapper) audioPlayer;
    this.buffer = ByteBuffer.allocate(1024);
    this.frame = new MutableAudioFrame();
    this.frame.setBuffer(buffer);
}
 
Example #18
Source File: PlayerPauseEvent.java    From Lavalink-Client with MIT License 4 votes vote down vote up
public PlayerPauseEvent(IPlayer player) {
    super(player);
}
 
Example #19
Source File: TrackStuckEvent.java    From Lavalink-Client with MIT License 4 votes vote down vote up
public TrackStuckEvent(IPlayer player, AudioTrack track, long thresholdMs) {
    super(player);
    this.track = track;
    this.thresholdMs = thresholdMs;
}
 
Example #20
Source File: TrackEndEvent.java    From Lavalink-Client with MIT License 4 votes vote down vote up
public TrackEndEvent(IPlayer player, AudioTrack track, AudioTrackEndReason reason) {
    super(player);
    this.track = track;
    this.reason = reason;
}
 
Example #21
Source File: PlayerEvent.java    From Lavalink-Client with MIT License 4 votes vote down vote up
public PlayerEvent(IPlayer player) {
    this.player = player;
}
 
Example #22
Source File: PlayerEvent.java    From Lavalink-Client with MIT License 4 votes vote down vote up
public IPlayer getPlayer() {
    return player;
}
 
Example #23
Source File: TrackStartEvent.java    From Lavalink-Client with MIT License 4 votes vote down vote up
public TrackStartEvent(IPlayer player, AudioTrack track) {
    super(player);
    this.track = track;
}
 
Example #24
Source File: TrackExceptionEvent.java    From Lavalink-Client with MIT License 4 votes vote down vote up
public TrackExceptionEvent(IPlayer player, AudioTrack track, Exception exception) {
    super(player);
    this.track = track;
    this.exception = exception;
}
 
Example #25
Source File: PlayerEventListenerAdapter.java    From Lavalink-Client with MIT License 4 votes vote down vote up
/**
 * @param player Audio player
 */
public void onPlayerPause(IPlayer player) {
    // Adapter dummy method
}
 
Example #26
Source File: PlayerEventListenerAdapter.java    From Lavalink-Client with MIT License 4 votes vote down vote up
/**
 * @param player Audio player
 */
public void onPlayerResume(IPlayer player) {
    // Adapter dummy method
}
 
Example #27
Source File: PlayerResumeEvent.java    From Lavalink-Client with MIT License 4 votes vote down vote up
public PlayerResumeEvent(IPlayer player) {
    super(player);
}
 
Example #28
Source File: PlayerEventListenerAdapter.java    From Lavalink-Client with MIT License 2 votes vote down vote up
/**
 * @param player Audio player
 * @param track Audio track that started
 */
public void onTrackStart(IPlayer player, AudioTrack track) {
    // Adapter dummy method
}
 
Example #29
Source File: PlayerEventListenerAdapter.java    From Lavalink-Client with MIT License 2 votes vote down vote up
/**
 * @param player Audio player
 * @param track Audio track that ended
 * @param endReason The reason why the track stopped playing
 */
public void onTrackEnd(IPlayer player, AudioTrack track, AudioTrackEndReason endReason) {
    // Adapter dummy method
}
 
Example #30
Source File: PlayerEventListenerAdapter.java    From Lavalink-Client with MIT License 2 votes vote down vote up
/**
 * @param player Audio player
 * @param track Audio track where the exception occurred
 * @param exception The exception that occurred
 */
public void onTrackException(IPlayer player, AudioTrack track, Exception exception) {
    // Adapter dummy method
}