I am trying to run 4 MP3 tracks at the same time in different threads. I am using the JLayer1.0 MP3 library to play the MP3s. Given that I cannot control when the threads will start, I am using a CountDownLatch to at least get them to run at the same time. However, every time I run the program, the tracks will play, but will consistently start at different times causing them to be off timing.
Here's my program:
public class MP3 {
private String filename;
private Player player;
private final CountDownLatch runlatch;
// constructor that takes the name of an MP3 file
public MP3(String filename, CountDownLatch runlatch) {
this.filename = filename;
this.runlatch = runlatch;
}
// play the MP3 file to the sound card
public void play() {
try {
FileInputStream fis = new FileInputStream(filename);
BufferedInputStream bis = new BufferedInputStream(fis);
System.out.println(filename);
player = new Player(bis);
}
catch (Exception e) {
System.out.println("Problem playing file " + filename);
System.out.println(e);
}
// run in new thread to play in background
Thread track = new Thread() {
public void run() {
try {
try {
runlatch.countDown();
runlatch.await();
player.play();
} catch (InterruptedException e) {
System.out.println(e);
}
}
catch (Exception e) { System.out.println(e); }
}
};
track.start();
}
// test client
public static void main(String[] args) throws InterruptedException {
CountDownLatch runlatch = new CountDownLatch(4);
String filename1 = args[0];
String filename2 = args[1];
String filename3 = args[2];
String filename4 = args[3];
MP3 track1 = new MP3(filename1, runlatch);
MP3 track2 = new MP3(filename2, runlatch);
MP3 track3 = new MP3(filename3, runlatch);
MP3 track4 = new MP3(filename4, runlatch);
track1.play();
track2.play();
track3.play();
track4.play();
}
}
I know that I cannot control how each thread executes the code after the latch is opened by the final thread. It seems that I would have to get into the JLayer1.0 implementation to have more control of when the MP3s will start to play.
Is there any simple way to get the MP3 tracks to stay in timing throughout their duration? I know a multiple track player has already been implemented in java, but it seems to be more complex then what I want.