2014-12-20 2 views
1

안녕하세요. 내 응용 프로그램에 적합한 사운드 클래스가 있으며, 내가 말할 때 특정 사운드를 재생합니다.Java 소리 감지시 감지하기

public class Sound { 

public static final Sound cash = new Sound("/cash.wav"); 
public static final Sound snap = new Sound("/snap.wav"); 
public static final Sound disarm = new Sound("/disarm.wav"); 
public static final Sound tp = new Sound("/tp.wav"); 
public static final Sound select = new Sound("/selectBTN.wav"); 
public static final Sound scroll = new Sound("/btn.wav"); 
public static final Sound fire = new Sound("/fire2.wav"); 

private AudioClip c; 

public Sound(String filename) { 
    try { 
     c = Applet.newAudioClip(Sound.class.getResource(filename)); 

    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

public void play() { 
    try { 
     new Thread() { 
      public void run() { 
       if (!title.mute) { 

        c.play(); 
       } 
      } 
     }.start(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

}

후 내가 사용하는 사운드를 재생 : 나는 소리가 그래서 여기에 래핑을 통해 그들없이 다른 사운드를 재생할 수있는 재생 완료 될 때 감지 할 수 있기를 원하는 것은 내 사운드 클래스 이 코드 줄 : 사운드가이 같은 (AN aproximation이다)를 시도

답변

1

재생 완료되면

Sound.cash.play(); 

이 어떻게 감지 할 수되는 LineListener과은을 감지 연주의 말 :

import javax.sound.sampled.AudioInputStream; 
    import javax.sound.sampled.AudioSystem; 
    import javax.sound.sampled.Clip; 
    import javax.sound.sampled.LineEvent; 
    import javax.sound.sampled.LineEvent.Type; 
    import javax.sound.sampled.LineListener; 

    public class Sound { 


     private Clip     c; 

     public Sound(final String filename) { 
      try { 
       c = AudioSystem.getClip(); 
       final AudioInputStream inputStream = AudioSystem.getAudioInputStream(Sound.class.getResourceAsStream(filename)); 
       c.open(inputStream); 
       c.addLineListener(new LineListener() { 

        @Override 
        public void update(final LineEvent event) { 
         if (event.getType().equals(Type.STOP)) { 
          System.out.println("Do something"); 
         } 
        } 
       }); 
      } catch (final Exception e) { 
       e.printStackTrace(); 
      } 
     } 

     public void play() { 
      c.start(); 
     } 

     public static void main(final String[] args) throws InterruptedException { 
      final Sound s = new Sound("/cash.wav"); 
      s.play(); 
      Thread.sleep(100000); 
      final Sound p = new Sound("/cash.wav"); 
      p.play(); 
      Thread.sleep(10000); 
     } 
    } 
+0

이 작동하지만 몇 가지 이유에 대해 한 번 내가이 – user2279603

+0

그것의이 같은 사운드 객체를 재사용 할 것으로 보인다 해결할 수있는 방법은 늘 다시 재생 사운드를 재생, 참조 http://stackoverflow.com/questions/9999961/song-plays-first-time-but-not-play-once-stopped-clip-in-java – rafalopez79