2

我试图在不同的线程中同时运行 4 个 MP3 曲目。我正在使用JLayer1.0 MP3 库来播放 MP3。鉴于我无法控制线程何时启动,我使用 CountDownLatch 至少让它们同时运行。但是,每次我运行程序时,曲目都会播放,但会始终在不同的时间开始,导致它们不定时。

这是我的程序:

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();
    }
}

我知道在最后一个线程打开闩锁后,我无法控制每个线程如何执行代码。看来我必须进入 JLayer1.0 实现才能更好地控制 MP3 何时开始播放。

有没有什么简单的方法可以让 MP3 曲目在整个播放过程中保持同步?我知道已经在 java 中实现了一个多轨播放器,但它似乎比我想要的更复杂。

4

1 回答 1

1

阅读 CountdownLatch 的 API 让我想知道您是否可以按照您尝试使用它的方式使用它。

“一种同步辅助,允许一个或多个线程等待,直到在其他线程中执行的一组操作完成。”

CountdownLatch 示例中的 run 方法如下所示:

public void run() {
      try {
        startSignal.await();
        doWork();
        doneSignal.countDown();
      } catch (InterruptedException ex) {} // return;
  }

它等待开始信号,但直到它的工作方法完成后才进行倒计时。您似乎误用了 CountdowLatch,其真正目的是确保在主线程完成之前完成所有线程。

所以这是我的建议:像你正在做的那样启动每个线程,但不要使用 CountdownLatch,而是让每个线程在静态布尔值上循环,直到值变为 false。这样,你启动了所有线程,然后你可以将你的 ThreadLocal 设置为 false,它们都应该调用 play()。请注意,这可能不是生产质量的代码,您确实应该让每个线程都侦听一个事件,但请试一试。

总体问题可能是线程的操作系统调度是罪魁祸首,这就是 JPlayer 1.0 开发人员想出的解决方案,但如果硬件和操作系统正在合作,它应该能够让 play() 到每个线程同时发生加或减几毫秒。

最后编辑:我越看 CountdownLatch,我认为您可以使用它来同步启动线程的次数越多。您不希望每个线程中的倒计时,您只想要:

startSignal.await();
play();
于 2012-06-17T14:53:18.637 回答