Hi all!
I'm playing to start a simple mp3 player written in Java, with a very simply GUI (still deciding between Swing or SWT). But anyway, I'm not going very far. My first thought was to use
JLayer. The problem is actually to find a simple example of how to play a file. Something "dummy-safe", but nope. I tried to hack a bit myself and I came up with this:
Code:
import java.io.File;
import java.io.IOException;
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.UnsupportedAudioFileException;
import javazoom.jl.decoder.JavaLayerException;
import javazoom.jl.player.*;
public class MainClass {
private Player player;
public MainClass() throws UnsupportedAudioFileException, IOException, JavaLayerException {
/** 44100Hz, 16-bit, stereo, signed, little-endian */
AudioFormat playbackFormat = new AudioFormat(44100, 16, 2, true, false);
AudioInputStream source = AudioSystem.getAudioInputStream(new File(
"test.mp3"));
source = AudioSystem.getAudioInputStream(playbackFormat, source);
player = new Player(source);
System.out.println("playing anything?");
player.play();
System.out.println("don't know...");
}
public static void main(String[] args) throws UnsupportedAudioFileException, IOException, JavaLayerException {
new MainClass();
}
}
I know the above code is horrible. This is just a simple test to know if I can or not get mp3 to play. Trying to run the above code (and yes, I do have a file called "test.mp3" in a valid place inside my project map), does well... nothing. I've two System.out.println() in the above code. When the application runs, The fist System.out runs and after a few seconds, the second System.out runs. I'm totally missing what is going on here. Should this piece of code be put in a separated thread?
I've two jar files in this project:
jl1.0.jar(This is JLayer from
www.javazoom.net) and
mp3plugin.jar(from
Sun's homepage).
Since this was not working, I went ahead and decided to give SDL a shot, but it is not going well. I've worked before with midi playback in the J2SE and I used javaSDL as well for graphics only. Getting sound to work seems harder than graphics so far ^_^;;
So, if anyone has any tips about mp3 playback in Java, please give me some ideas. Please, don't post links such as
jlGUI or
JavaTunes because those are far too advanced mp3 players and will do little to help me. I simply need some simple examples of how to use JLayer (or any other simple to use API).
Thanks in advance for any help!
P.S: I won't put any SDL code here because I would ratter prefer to use JLayer in this case. SDL only if I can't fix this