001package com.ganteater.ae.desktop.util; 002 003import javax.sound.sampled.AudioInputStream; 004import javax.sound.sampled.AudioSystem; 005import javax.sound.sampled.Clip; 006 007import com.ganteater.ae.ILogger; 008import com.ganteater.ae.Logger; 009import com.ganteater.ae.desktop.ui.AEFrame; 010 011public class SoundManager { 012 013 private ILogger log = new Logger("SoundManager"); 014 015 private static Clip clip; 016 private static Thread thread = null; 017 018 private AEFrame aeFrame; 019 private long[] delaysBeforePlay = { 2000, 30000, 30000, 30000, 30000, 5000, 10000 }; 020 021 public SoundManager(AEFrame aeFrame) { 022 this.aeFrame = aeFrame; 023 } 024 025 synchronized public void play() { 026 if (thread == null) { 027 if (aeFrame.isUserInactive()) { 028 thread = new Thread(() -> { 029 AudioInputStream audioInputStream; 030 try { 031 int i = 1; 032 for (long delay : delaysBeforePlay) { 033 if (thread == null) { 034 break; 035 } 036 Thread.sleep(delay); 037 if (thread == null) { 038 break; 039 } 040 audioInputStream = AudioSystem 041 .getAudioInputStream(AEFrame.class.getResource("/sounds/anteater-" + i + ".wav")); 042 clip = AudioSystem.getClip(); 043 clip.open(audioInputStream); 044 clip.start(); 045 i++; 046 } 047 } catch (InterruptedException e) { 048 //do nothing. 049 } catch (Exception e1) { 050 log.debug("Notification sounds error: " + e1.getMessage()); 051 } 052 }); 053 054 thread.start(); 055 } 056 } 057 } 058 059 public static void stop() { 060 if (thread != null) { 061 synchronized (thread) { 062 thread.interrupt(); 063 } 064 } 065 thread = null; 066 if (clip != null) { 067 clip.stop(); 068 } 069 clip = null; 070 } 071}