다른 스레드에서 4 개의 MP3 트랙을 동시에 실행하려고합니다. MP3를 재생하려면 JLayer1.0 MP3 library을 사용하고 있습니다. 스레드가 시작될시기를 제어 할 수 없다는 것을 감안할 때 CountDownLatch를 사용하여 적어도 동시에 실행할 수있게합니다. 그러나 프로그램을 실행할 때마다 트랙이 재생되지만 서로 다른 시간에 일관되게 시작되어 타이밍이 어긋납니다.다른 스레드에서 여러 MP3 트랙 재생
여기 내 프로그램입니다 :
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();
}
}
내가 래치가 최종 스레드에 의해 열린 후 각 스레드가 코드를 실행하는 방법을 제어 할 수없는 것을 알고있다. MP3 재생이 시작될시기를 더 잘 제어하려면 JLayer1.0 구현에 들어가야 할 것 같습니다.
MP3 트랙이 지속되는 동안 타이밍을 유지하는 간단한 방법이 있습니까? 나는 multiple track player이 이미 자바로 구현되었다는 것을 알고 있지만, 내가 원하는 것보다 더 복잡해 보인다.
그냥 생각해보십시오. 어쩌면 다중 트랙 재생은 원하는만큼 복잡 할 수 있습니다. 스레드는 쉽게 제어되지 않고 쉽게 동기화되지 않습니다. 이 일이 자동으로 수행된다면, 고통을 잊어 버려라. 누군가 이미 알아 냈어. (그리고 다시,이 퍼즐들이 당신을 즐겁게한다면, 행운을 빈다) –