Search code examples
javaherokubufferbytebufferdiscord-jda

Why does heroku give me this "incompatible type" error?


I am trying to deploy my discord bot on Heroku.com, however i recieve a strange error... Here's the code of the class that throws the error:

package main.Events.lavaplayer;

import com.sedmelluq.discord.lavaplayer.player.AudioPlayer;
import com.sedmelluq.discord.lavaplayer.track.playback.MutableAudioFrame;
import net.dv8tion.jda.api.audio.AudioSendHandler;
import org.jetbrains.annotations.Nullable;

import java.nio.ByteBuffer;

public class AudioPlayerSendHandler implements AudioSendHandler {
    private final AudioPlayer audioPlayer;
    private final ByteBuffer buffer;
    private final MutableAudioFrame frame;

    public AudioPlayerSendHandler(AudioPlayer audioPlayer) {
        this.audioPlayer = audioPlayer;
        this.buffer = ByteBuffer.allocate(1024);
        this.frame = new MutableAudioFrame();
        this.frame.setBuffer(buffer);
    }

    @Override
    public boolean canProvide() {
        return this.audioPlayer.provide(this.frame);
    }

    @Nullable
    @Override
    public ByteBuffer provide20MsAudio() {
        return this.buffer.flip();
    }

    @Override
    public boolean isOpus() {
        return true;
    }
}

This piece of code doesn't give me any problems while compiling with Intellij, but when i try to deploy it to Heroku they give me this error:

error: incompatible types: java.nio.Buffer cannot be converted to @org.jetbrains.annotations.Nullable java.nio.ByteBuffer return this.buffer.flip();


Solution

  • flip returns a Buffer rather than ByteBuffer so a cast is needed

    public ByteBuffer provide20MsAudio() {
        return (ByteBuffer) buffer.flip();
    }