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

后端 未结 5 1891
闹比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条回答
  •  攒了一身酷
    2020-11-29 10:27

    As of JDK8, Clip's listener is kind of buggy (i.e. it can happen that it fires the Type.STOP twice for a single reproduction of the audio (.start()).

    I would therefore use something like:

    Clip[] myBunchOfClipsToPlaySequentially=new Clip[A_BUNCH];
    //
    // [load actual clips...]
    // [...]
    //
    for(Clip c: myBunchOfClipsToPlaySequentially)
        while(c.getFramePosition()

    In addition, you could take a look to the new AudioClip class that JFX8 provides (has some nifty methods to fiddle with)

提交回复
热议问题