简体   繁体   中英

Clip.LOOP_CONTINUOUSLY not working

please help me figure out why my wav file is not looping and how do I call this class on my mousepressed event on a different class? Thanks in advance.

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;

import sun.audio.AudioPlayer;

public class Test2 {
    {
    new Thread(new Runnable() {
      public void run() {
        try {

          Clip clip = AudioSystem.getClip();
          AudioInputStream inputStream = AudioSystem.getAudioInputStream(AudioPlayer.class.getResourceAsStream("ATS.wav"));         
          clip.open(inputStream);
          clip.loop(Clip.LOOP_CONTINUOUSLY); 
        } catch (Exception e) {
          System.err.println(e.getMessage());
        }
      }
    }).start();  
  }     
}

In order to loop it, you must start it.

clip.start();

To call it from another location, use new Test2(); .

你需要一个 Thread 可能是像这样的:

Thread.sleep(clip.getMicrosecondLength()/1000); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM