I am using the following code to play a sound file using the java API.
Clip clip = AudioSystem.getClip(); AudioInputStream inputStream = AudioSystem.getAudioInputStream(stream); clip.open(inputStream); clip.start();
The method of calling the Clip.start () method immediately returns, and the system plays the sound file in the background stream. I want my method to pause until playback finishes.
Is there a good way to do this?
EDIT: for anyone interested in my final solution, based on a response from Uri, I used the following code:
private final BlockingQueue<URL> queue = new ArrayBlockingQueue<URL>(1); public void playSoundStream(InputStream stream) { Clip clip = AudioSystem.getClip(); AudioInputStream inputStream = AudioSystem.getAudioInputStream(stream); clip.open(inputStream); clip.start(); LineListener listener = new LineListener() { public void update(LineEvent event) { if (event.getType() != Type.STOP) { return; } try { queue.take(); } catch (InterruptedException e) {
java javasound
Panagiotis korros
source share