how can I wait for a java sound clip to finish playing back?

后端 未结 5 1917
闹比i
闹比i 2020-11-29 10:00

I am using the following code to play a sound file using the java sound API.

    Clip clip = AudioSystem.getClip();
    AudioInputStream inputStream = AudioS         


        
5条回答
  •  猫巷女王i
    2020-11-29 10:18

    you can just put this code instead:

    assume your clip1 is playing and you want a clip2 to be played right after that, you can:

    clip1.start();
    while(clip1.getMicrosecondLength() != clip1.getMicrosecondPosition())
    {
    }
    clip2.loop(some int here);
    

    and, to make this work without delaying your main task (I say this because the while loop makes the work wait for clip1 to finish, no matter what the next work is...) you can make a new thread in the point where you want it to happen, and just put the code in its run() method... GOOD LUCK!

提交回复
热议问题