question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Javacord bot fails to play audio in a voice channel.

See original GitHub issue

When I run my command to join the voice channel and play audio then disconnect, I get this error:

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
2021-05-23 15:01:34.429-0700 INFO org.javacord.core.util.gateway.AudioWebSocketAdapter Websocket closed with reason 'unknown' and code NORMAL (1000) by client for AudioConnection (channel: ServerVoiceChannel (id: 760350712353259543, name: General), sessionId: d41a8cfc709285063c6ae30a34dd9806, endpoint: us-west7144.discord.media:443)! {shard=0} 
2021-05-23 15:01:34.498-0700 WARN org.javacord.core.util.handler.guild.VoiceStateUpdateHandler Couldn't get the Channel with the id 0 for a org.javacord.core.util.handler.guild.VoiceStateUpdateHandler. Please update to the latest Javacord version or create an issue on the Javacord GitHub page if you are already on the latest version. {shard=0}

Code:

ServerVoiceChannel sVC = event.getApi().getServerVoiceChannelById(760350712353259543L).get();
sVC.connect().thenAccept(audioConnection -> {
    // Create a player manager
    AudioPlayerManager playerManager = new DefaultAudioPlayerManager();
    playerManager.registerSourceManager(new YoutubeAudioSourceManager());
    AudioPlayer player = playerManager.createPlayer();

    // Create an audio source and add it to the audio connection's queue
    AudioSource source = new LavaPlayerAudioSource(event.getApi(), player);
    audioConnection.setAudioSource(source);

    // Use AudioPlayer like normal LavaPlayer
    playerManager.loadItem("https://youtu.be/YAPzjHObF1A", new AudioLoadResultHandler() {
        @Override
        public void trackLoaded(AudioTrack track) {
            player.playTrack(track);
        }

        @Override
        public void playlistLoaded(AudioPlaylist playlist) {
            for (AudioTrack track : playlist.getTracks()) {
                player.playTrack(track);
            }
        }

        @Override
        public void noMatches() {
            event.getChannel().sendMessage("No matches :(");
        }

        @Override
        public void loadFailed(FriendlyException exception) {
            event.getChannel().sendMessage("Load failed :(");
        }
    });
    audioConnection.close();
});

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:10 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
ShindouMihoucommented, Jun 18, 2021

I’ll explain it better in his/her place, he/she means your code is triggering AudioConnection#close after selecting a song which basically closes the audio connection between your bot and Discord which in turns makes your bot leave.

The close method is located at the end of the excerpt code you showed.

0reactions
Macintosh-Fancommented, Jul 17, 2021

Well, I did get the audio to successfully play, so it’s all good now.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Playing Audio - Javacord
Javacord allows your bot to connect to voice channels and play audio (e.g., music). This short tutorial gives you an introduction on how...
Read more >
Discord Java Bot (with Javacord library) can't play music on ...
I'm trying to create a Discord Bot with Java, using Javacord and Lavaplayer librairies, to play music on voice channels, but I'm having ......
Read more >
Audio support · Issue #390 · Javacord/Javacord - GitHub
If the bot is already connected to another voice channel on the same server, it will leave the old channel and join the...
Read more >
Connecting your Discord Bot to Voice Channels with Java and ...
Hello there, and welcome to the tutorial on connecting/disconnecting bots to and from voice channels! Whilst it does not sound very exciting ...
Read more >
VoiceConnection | Eris 0.17.1 - Abal.moe
The ID of the voice connection's current channel. type String .connecting ... Play an audio or video resource. ... Switch the voice channel...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found