LWJGL Forum

Programming => OpenAL => Topic started by: Cottonwood on December 18, 2010, 12:01:33

Title: [solved] How can I play mp3 and ogg in one java player program?
Post by: Cottonwood on December 18, 2010, 12:01:33
Slick-Util can play ogg & mp3, so you're good there.

With your help I could realize playing ogg sounds. But I can't find anything about slick and mp3. Google (http://www.google.de/search?num=20&hl=de&lr=&newwindow=1&client=firefox-a&rls=org.mozilla%3Ade%3Aofficial&channel=s&as_qdr=all&q=mp3+site%3Ahttp%3A%2F%2Fslick.cokeandcode.com%2F&btnG=Suche&aq=f&aqi=&aql=&oq=&gs_rfai=)

Background
I have a program that can play mp3 (only) in a windows environment. But it couldn't play ogg. So I thought I had to find out how to play ogg.
As I could that, I thought I could combine both in one program. But the mp3 runs only without the slick libraries.
As soon as I added the slick libraries to the eclipse runtime environment I got an error message when playing mp3. Without any change within the code.
Code: [Select]
"file:///D:/Musik/Musik-DVD1/Albert Hammond - Down By The River.mp3"
  Unable to handle format: mpeglayer3, 44100.0 Hz, 16-bit, Mono, LittleEndian, Signed, 16000.0 frame rate, FrameSize=16384 bits
Failed to realize: com.sun.media.PlaybackEngine@118f375
Error: Unable to realize com.sun.media.PlaybackEngine@118f375
java.lang.NullPointerException
at MusicPlayer.run(MusicPlayer.java:114)
at java.lang.Thread.run(Unknown Source)
Title: Re: How can I play mp3 and ogg in one java player program?
Post by: Matthias on December 18, 2010, 12:42:55
Just use the mp3 decoder library (like jlayer3) and stream the sound through OpenAL.
Title: Re: How can I play mp3 and ogg in one java player program?
Post by: Cottonwood on December 18, 2010, 13:08:38
Sorry, but I don't understand what you're meaning. I can play mp3, but only without slick libraries. And I can play ogg/vorbis, but only with slick libraries. And that doesn't fall into place.
Title: Re: How can I play mp3 and ogg in one java player program?
Post by: Matthias on December 18, 2010, 13:31:47
Playing music works like this:
- you have a decoder library which converts bytes to audio samples
- you have a audio playback library to play these audio samples

LWJGL offers OpenAL for audio playback and also recording.
Slick(-Utils) just offers some wrapper around OpenAL and some decoders.

You can always use OpenAL directly without Slick(-Utils) or you can use or extend Slick-(Utils) code to playback your music.
Title: Re: How can I play mp3 and ogg in one java player program?
Post by: Cottonwood on December 18, 2010, 14:00:47
Sorry, but I think we don't understand each other. How can I play mp3 with slick or ogg without?
Title: Re: How can I play mp3 and ogg in one java player program?
Post by: Cottonwood on December 20, 2010, 23:51:05
Now I can play mp3 as well as ogg without changing anything. What I did before (what I wrote about) was to change the VM arguments as you can see on the shot 1. With the -Djava.library.path=..\lwjgl\native\windows it worked for ogg, without it worked for mp3. That wasn't really a solution.

After some tests I found out that it works for both when I copy the modules from -Djava.library.path=..\lwjgl\native\windows directly into the working directory and without setting VM arguments.

That brought me to the idea to set the working directory to the -Djava.library.path=..\lwjgl\native\windows without copying libs and without setting the VM arguments. And that also works. For me it is the best solution I can get at the moment. (shot 2)
Title: Re: [solved] How can I play mp3 and ogg in one java player program?
Post by: jediTofu on December 21, 2010, 05:04:21
You should be linking your libraries anyway to use LWJGL (i.e., -Djava.library.path=<path>).

Don't make the same mistake I did; don't rely on working directory.  Linux's JVM, for whatever reason, doesn't include the current directory in java.library.path automatically.
For distribution, use JNLP, Applets, or if you really have to OS-specific stuff (microsoft batch scripts, shell/bash unix scripts, or online programs that can make exe's/elf's or by using gcj compiler).
Title: Re: How can I play mp3 and ogg in one java player program?
Post by: Cottonwood on December 21, 2010, 06:37:26
Title: Re: [solved] How can I play mp3 and ogg in one java player program?
Post by: broumbroum on December 21, 2010, 20:30:34
CotonWood, you seem to ask for how to load natives from an external path than the LWJGL/SLick one. I think linux and windows have a common library path you can use, it is the user.home directory. Anyway, if it is not the case, System.load() can help ( http://download.oracle.com/javase/1.4.2/docs/api/java/lang/System.html#load(java.lang.String) ). it can load a library located at any folder you want.

[I should have put it in another Thread, because it is maybe off-topic so here's the end of this post (http://lwjgl.org/forum/index.php/topic,3646.0.html)]
Title: Re: [solved] How can I play mp3 and ogg in one java player program?
Post by: broumbroum on December 21, 2010, 20:40:37
Mp3 is playing fine with JLayer. I see a JOrbis library that can play ogg, too. http://www.jcraft.com/jorbis/.