wangjunxiao

参考java官网关于此问题的解释

播放短的音频时,在还没有播放时,程序就调用了stop事件,解决方法,为Clip添加一个监听器:

 1 import javax.sound.sampled.*;
 2 
 3 public class EarlyStopDemo {
 4     private Clip clip;
 5     
 6     public void go() throws Exception {
 7         AudioInputStream audioInputStream =
 8             AudioSystem.getAudioInputStream(
 9                 new java.io.File("testLong.wav"));
10         DataLine.Info info =
11                 new DataLine.Info(Clip.class, audioInputStream.getFormat());
12         clip = (Clip) AudioSystem.getLine(info);
13         clip.addLineListener(new LineListener() {
14                 public void update(LineEvent e) {
15                     if (e.getType() == LineEvent.Type.STOP) {
16                         synchronized(clip) {
17                             clip.notify();
18                         }
19                     }
20                 }
21             });
22         clip.open(audioInputStream);
23         clip.start();
24         synchronized (clip) {
25             clip.wait();
26         }
27         clip.close();
28     }
29 
30     public static void main(String[] args) throws Exception {
31         EarlyStopDemo acd = new EarlyStopDemo();
32         for (int i = 0; i < 10 ; i++ ) {
33             acd.go();
34         }
35     }
36 }

 

分类:

技术点:

相关文章:

  • 2021-10-30
  • 2021-08-15
  • 2021-12-05
  • 2021-06-12
  • 2022-12-23
  • 2021-11-17
  • 2021-06-17
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2021-10-16
  • 2021-09-09
  • 2021-05-04
  • 2021-12-20
  • 2022-12-23
  • 2021-11-17
相关资源
相似解决方案