提问者:小点点

不和谐语音频道中未播放音乐


我正在尝试将播放音乐的功能添加到我的自定义不和谐机器人中。我正在Java11、Spring Boot、Discord4j和LavaPlayer库中编写它。

我有命令!join,它使Discordbot加入输入命令的人的语音通道。这是可行的,但一旦机器人加入,我将LavaPlayerAudioProvider分配给VoiceState。所以错误可能发生在这里(我不知道)。这是!join命令的类:

@RequiredArgsConstructor
public class JoinCommandHandler implements CommandStrategy {
    private final LavaPlayerAudioProvider provider;

    /**
     * {@inheritDoc}
     * <p>
     * Makes the bot join the voice channel of the user.
     */
    @Override
    public Mono<Void> execute(Message message) {
        return Mono.just(message)
                .flatMap(Message::getAuthorAsMember)
                .flatMap(m -> m.getVoiceState()
                        .flatMap(VoiceState::getChannel))
                .flatMap(c -> c.join(spec -> spec.setProvider(provider)))
                .then();
    }

    @Override
    public String getCommandName() {
        return "!join";
    }

    @Override
    public String getDescription() {
        return "**Under Development** Make this bot join your channel.";
    }
}

老实说,我不认为错误会出现在!join命令,但更多地理解这个问题可能是有用的。

我在AudioConfig类中设置了音频配置,如下所示:

@Configuration
public class AudioConfig {
    @Bean
    public AudioPlayerManager audioPlayerManager() {
        final AudioPlayerManager playerManager = new DefaultAudioPlayerManager();
        playerManager.getConfiguration().setFrameBufferFactory(NonAllocatingAudioFrameBuffer::new);
        AudioSourceManagers.registerLocalSource(playerManager);
        return playerManager;
    }

    @Bean
    public AudioPlayer audioPlayer() {
        return audioPlayerManager().createPlayer();
    }

    @Bean
    public AudioProvider audioProvider() {
        return new LavaPlayerAudioProvider(audioPlayer());
    }
}

我写了一个!一旦机器人加入语音频道,播放[youtube url]命令来播放音乐。

@Service("!play")
@RequiredArgsConstructor
public class PlayCommand implements CommandStrategy {
    private final AudioPlayerManager manager;
    private final TrackScheduler trackScheduler;

    /**
     * {@inheritDoc}
     * <p>
     * Makes the bot queue a song and play in the channel.
     */
    @Override
    public Mono<Void> execute(Message message) {
        return Mono.just(message.getContent())
                .map(content -> Arrays.asList(content.split(" ")))
                .doOnNext(command -> manager.loadItem(command.get(1), trackScheduler)).then();
    }
}

这里的LavaPlayerAudioProviver提供音乐:

@Component
public final class LavaPlayerAudioProvider extends AudioProvider {
    private final AudioPlayer audioPlayer;
    private final MutableAudioFrame frame = new MutableAudioFrame();

    public LavaPlayerAudioProvider(AudioPlayer audioPlayer) {
        super(ByteBuffer.allocate(StandardAudioDataFormats.DISCORD_OPUS.maximumChunkSize()));
        this.frame.setBuffer(getBuffer());
        this.audioPlayer = audioPlayer;
    }

    @Override
    public boolean provide() {
        final boolean didProvide = audioPlayer.provide(frame);
        if (didProvide) {
            getBuffer().flip();
        }
        return didProvide;
    }
}

现在棘手的部分来了。我调试了代码,以逐步查看代码在我键入! play[url]命令后是如何执行的。

当代码到达TrackScheduler类时,它不会执行trackLoed()方法来播放歌曲,而是执行noMatches()方法,这基本上意味着找不到URL,因此无法播放音乐。你可以在这里找到TrackScheduler类:

@Component
@RequiredArgsConstructor
public class TrackScheduler implements AudioLoadResultHandler {
    private final AudioPlayer player;

    @Override
    public void trackLoaded(final AudioTrack track) {
        // LavaPlayer found an audio source for us to play
        player.playTrack(track);
    }

    @Override
    public void playlistLoaded(final AudioPlaylist playlist) {
        // LavaPlayer found multiple AudioTracks from some playlist
    }

    @Override
    public void noMatches() {
        // LavaPlayer did not find any audio to extract
    }

    @Override
    public void loadFailed(final FriendlyException exception) {
        // LavaPlayer could not parse an audio source for some reason
    }
}

我尝试了不同的URL,但它们最终都使用了noMatch()方法…我遵循了Discord4J指南(https://docs.discord4j.com/music-bot-tutorial),但它们没有使用Spring Boot,所以这有点令人困惑…

如果你需要更多的信息,我很乐意给你。提前感谢伙计们!


共1个答案

匿名用户

好吧,我知道有点晚了,但干得好。

你的问题是,你只注册了音频播放器的本地资源。这意味着,拉瓦播放器不知道什么是像脸谱网、YouTube或Vimeo这样的网站,也不知道如何处理它们。

您应该添加AudioSourceManager. regsterRemoteS的(audioPlayerManager);到您的配置类。

所以最终的配置类将如下所示:

@Configuration
public class AudioConfig {
    @Bean
    public AudioPlayerManager audioPlayerManager() {
        final AudioPlayerManager playerManager = new DefaultAudioPlayerManager();
        playerManager.getConfiguration().setFrameBufferFactory(NonAllocatingAudioFrameBuffer::new);
        AudioSourceManagers.registerLocalSource(playerManager);
        AudioSourceManagers.registerRemoteSources(audioPlayerManager);
        return playerManager;
    }

    @Bean
    public AudioPlayer audioPlayer() {
        return audioPlayerManager().createPlayer();
    }

    @Bean
    public AudioProvider audioProvider() {
        return new LavaPlayerAudioProvider(audioPlayer());
    }
}

除此之外,您的代码对我来说工作得很好。我建议删除AudioSourceManager. regsterLocalSource(playerManager);总之,我看不出有任何理由以这种方式公开服务器端文件。