how can I wait for a java sound clip to finish playing back?

I prefer this way in Java 8: CountDownLatch syncLatch = new CountDownLatch(1); try (AudioInputStream stream = AudioSystem.getAudioInputStream(inStream)) { Clip clip = AudioSystem.getClip(); // Listener which allow method return once sound is completed clip.addLineListener(e -> { if (e.getType() == LineEvent.Type.STOP) { syncLatch.countDown(); } }); clip.open(stream); clip.start(); } syncLatch.await();

Audio volume control (increase or decrease) in Java

If you’re using the Java Sound API, you can set the volume with the MASTER_GAIN control. import javax.sound.sampled.*; AudioInputStream audioInputStream = AudioSystem.getAudioInputStream( new File(“some_file.wav”)); Clip clip = AudioSystem.getClip(); clip.open(audioInputStream); FloatControl gainControl = (FloatControl) clip.getControl(FloatControl.Type.MASTER_GAIN); gainControl.setValue(-10.0f); // Reduce volume by 10 decibels. clip.start();

Join two WAV files from Java?

Here is the barebones code: import java.io.File; import java.io.IOException; import java.io.SequenceInputStream; import javax.sound.sampled.AudioFileFormat; import javax.sound.sampled.AudioInputStream; import javax.sound.sampled.AudioSystem; public class WavAppender { public static void main(String[] args) { String wavFile1 = “D:\\wav1.wav”; String wavFile2 = “D:\\wav2.wav”; try { AudioInputStream clip1 = AudioSystem.getAudioInputStream(new File(wavFile1)); AudioInputStream clip2 = AudioSystem.getAudioInputStream(new File(wavFile2)); AudioInputStream appendedFiles = new AudioInputStream( new SequenceInputStream(clip1, clip2), … Read more

Playing MP3 using Java Sound API

As mentioned, Java Sound does not support MP3 by default. For the types it does support in any specific JRE, check AudioSystem.getAudioFileTypes(). One way to add support for reading MP3 is to add the JMF based mp3plugin.jar1 to the application’s run-time class-path. That link is known to be less than entirely reliable. The Jar is … Read more