Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import javax.sound.sampled.AudioInputStream;
- import javax.sound.sampled.AudioSystem;
- import javax.sound.sampled.Clip;
- import javax.sound.sampled.LineEvent;
- import java.io.File;
- import java.util.concurrent.CountDownLatch;
- public class Main {
- public static void main(String[] args) throws InterruptedException {
- playSound("/home/dburyak/Downloads/CantinaBand3.wav");
- System.out.println("End");
- }
- public static void playSound(final String url) {
- try {
- Clip clip = AudioSystem.getClip();
- var playbackFinished = new CountDownLatch(1);
- // set listener first, before starting the pipeline
- clip.addLineListener(e -> {
- if (e.getType() == LineEvent.Type.STOP) {
- playbackFinished.countDown();
- }
- });
- File file = new File(url);
- AudioInputStream inputStream = AudioSystem.getAudioInputStream(file);
- clip.open(inputStream);
- clip.start();
- playbackFinished.await();
- } catch (Exception e) {
- System.err.println(e.getMessage());
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement