English 中文(简体)
失去线性听众活动
原标题:Line listener event is lost
  • 时间:2012-04-24 08:10:13
  •  标签:
  • java
  • audio

我试图利用 Java弹片物体发出信号。 我从头开始,我等着STOP活动才开始,直到我继续read。 我注意到,如果我离开了我的申请中可能出现的电话线索,则电线不会起作用,或者只起作用。

然而,大多数时间都是如此,每次大约50次,无论是《裁武条约》还是“STOP”活动都没有发射,造成目前的read望永远等待。

现在的问题是,我是否对使我松散事件的私刑有过错?

private static volatile boolean isPlaying = false;
private static final Object waitObject = new Object();

public static void playClip(...)

...

    Clip clip = (Clip) AudioSystem.getLine(...);

    clip.addLineListener(new LineListener() {
        public void update(LineEvent event) {
            if (event.getType() == LineEvent.Type.STOP) {
                event.getLine().close();
                synchronized (waitObject) {
                    isPlaying = false;                        
                    waitObject.notifyAll();
                }
            }
        }
    });


    // start playing clip
    synchronized (waitObject) { 
        isPlaying = true;
    }
    clip.start();

    // keep Thread running otherwise the audio output is stopped when caller thread exits
    try {
        while (isPlaying) {
            synchronized (waitObject) {
                waitObject.wait();                    
            }
        }
    } catch (InterruptedException e) {           
        e.printStackTrace();
    }
}

Here is the new version using CountDownLatch:

private static volatile CountDownLatch playingFinished = new CountDownLatch(1);

public static void playClip(...)

...
    Clip clip = (Clip) AudioSystem.getLine(...);
    clip.open(audioInputStream);

    // use line listener to take care of synchronous call
    clip.addLineListener(new LineListener() {
        public void update(LineEvent event) {
            if (event.getType() == LineEvent.Type.STOP) {
                event.getLine().close();
                playingFinished.countDown();
            }
        }
    });

    clip.start();

    try {
        playingFinished.await();
    } catch (InterruptedException e) {
        e.printStackTrace();
    }

    playingFinished = new CountDownLatch(1);


I didn t include the debugging statements, but they indicate that the thread hangs in playingFinished.await(); because the STOP event was not fired and playingFinished.countDown(); is never called.

问题回答

Do yourself a favor and rework their Code to use the CountDownLatch rather than the low-level mothers-notification AP. 那么,你的问题可能会自食其力。

public void playClip() throws Exception {
  final CountDownLatch playingFinished = new CountDownLatch(1);
  final Clip clip = (Clip) AudioSystem.getLine(...);
  clip.open(...);
  clip.addLineListener(new LineListener() {
    public void update(LineEvent event) {
      if (event.getType() == LineEvent.Type.STOP) {
        event.getLine().close();
        playingFinished.countDown();
      }
    }
  });
  clip.start();
  try {
    playingFinished.await();
  } catch (InterruptedException e) {
    Thread.currentThread().interrupt();
  }
}




相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签