lavaplayer icon indicating copy to clipboard operation
lavaplayer copied to clipboard

Cannot load local file when packaged in jar

Open scuty2000 opened this issue 3 years ago • 5 comments

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.

scuty2000 avatar May 02 '21 23:05 scuty2000

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.

ellienieuwdorp avatar May 04 '21 10:05 ellienieuwdorp

Terrible non-solution: Write the resource to a temporary location and play it as local file.

MCOfficer avatar May 04 '21 10:05 MCOfficer

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

ellienieuwdorp avatar May 07 '21 08:05 ellienieuwdorp

[...] I've been trying to read it using getClass().getResourceAsStream() [...] and passing the stream to playerManager.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.

lukellmann avatar Jun 05 '21 01:06 lukellmann

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]).

FreshSupaSulley avatar May 02 '22 01:05 FreshSupaSulley