lavaplayer
lavaplayer copied to clipboard
Cannot load local file when packaged in jar
Hi, i'm having trouble loading media files when using a compiled version.
Tried loading both from resources folder in jar (using getClass().getResource()
and providing the path to audioPlayerManager.loadItem()
) and loading from external folder (providing a path to external file).
The class can actually load the file and says it exists, but audioPlayerManager can't find it.
Same issue here, I've been trying to read it using getClass().getResourceAsStream()
(since I think the main problem is that there is no such thing as a 'file' within a jar) and passing the stream to playerManager.decodeTrack(new MessageInput(inputStream))
but I keep getting errors while reading the input stream, they mostly seem related to AudioTrackInfo trying to be read from the file with input.readUTF8()
and producing a nullpointer.
Terrible non-solution: Write the resource to a temporary location and play it as local file.
Terrible non-solution: Write the resource to a temporary location and play it as local file.
Ah, that's actually better than my current workaround which includes running it in a docker image always packaging the sound files in the same folder as the jar in the image. Thanks :P
[...] I've been trying to read it using
getClass().getResourceAsStream()
[...] and passing the stream toplayerManager.decodeTrack(new MessageInput(inputStream))
[...]
This does not work because AudioPlayerManager#decodeTrack()
is supposed to decode tracks that have been encoded using AudioPlayerManager#encodeTrack()
. It is meant as a way to serialize / deserialize tracks.
I found a solution.
AudioTrack track = new Mp3AudioTrack(new AudioTrackInfo("Closing Time", "Semisonic", 232608, "", false, ""), new NonSeekableInputStream(AudioHandler.class.getClassLoader().getResourceAsStream("closing.mp3"))); musicManager.scheduler.queue(track);
Load your music file as an input stream into an Mp3AudioTrack object (there are Mpeg, Ogg, and Wav options). I queued the AudioTrack directly in my TrackScheduler (adapted from [https://github.com/sedmelluq/lavaplayer/blob/master/demo-jda/src/main/java/com/sedmelluq/discord/lavaplayer/demo/jda/TrackScheduler.java]).