fork download
  1. import java.io.File;
  2. import java.io.IOException;
  3. import java.util.concurrent.CountDownLatch;
  4.  
  5. import javax.sound.sampled.AudioSystem;
  6. import javax.sound.sampled.Clip;
  7. import javax.sound.sampled.DataLine;
  8. import javax.sound.sampled.LineEvent;
  9. import javax.sound.sampled.LineListener;
  10. import javax.sound.sampled.LineUnavailableException;
  11. import javax.sound.sampled.UnsupportedAudioFileException;
  12.  
  13. public class Chime {
  14. public static void main(String[] args) throws InterruptedException {
  15. Chime chime = new Chime();
  16.  
  17. for (int i = 0; i < 6; i++) {
  18. chime.play(i);
  19. Thread.sleep(2000);
  20. }
  21. }
  22.  
  23. final static String soundFiles[] = {
  24. "C:\\Users\\a\\Desktop\\work\\decision1.wav",
  25. "C:\\Users\\a\\Desktop\\work\\decision2.wav",
  26. "C:\\Users\\a\\Desktop\\work\\decision3.wav" };
  27.  
  28. public void play(int number) {
  29. Thread chimeThread = new Thread(new Runnable() {
  30. public void run() {
  31. new Playing().playSound(soundFiles[number % 3]);
  32. }
  33. });
  34.  
  35. chimeThread.start();
  36. }
  37.  
  38. static class Playing {
  39. private final CountDownLatch latch = new CountDownLatch(1);
  40.  
  41. public void playSound(String file) {
  42. try (var ais = AudioSystem.getAudioInputStream(new File(file))) {
  43. var af = ais.getFormat();
  44. var info = new DataLine.Info(Clip.class, af);
  45.  
  46. try (var clip = (Clip) AudioSystem.getLine(info)) {
  47. clip.addLineListener(new LineListener() {
  48. @Override
  49. public void update(LineEvent event) {
  50. if (event.getType() == LineEvent.Type.STOP) {
  51. latch.countDown();
  52. }
  53. System.out.println(event.getType());
  54. }
  55. });
  56.  
  57. clip.open(ais);
  58. clip.start();
  59. try {
  60. latch.await();
  61. } catch (InterruptedException e) {
  62. throw new RuntimeException(e);
  63. }
  64. }
  65. throw new RuntimeException(e1);
  66. }
  67. }
  68. }
  69.  
  70. }
  71.  
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:13: error: class Chime is public, should be declared in a file named Chime.java
public class Chime {
       ^
1 error
stdout
Standard output is empty