Clip plays wav file with bad delay in java

I wrote a code that read a WAV file (about 80 mb in size) and plays it. The problem is that the sound plays poorly (extreme lags). Could you tell me what the problem is?

Here is my code: (I call the function doPlayinside the Jframe constructor)

private void doPlay(final String path) {
    try {
        stopPlay();
        InputStream is = new FileInputStream(path);
        InputStream bufferedIn = new BufferedInputStream(is);
        AudioInputStream ais = AudioSystem.getAudioInputStream(bufferedIn);
        AudioFormat format = ais.getFormat();
        // this is the value of format.
        // PCM_SIGNED 44100.0 Hz, 16 bit, stereo, 4 bytes/frame, little-endian
        DataLine.Info info = new DataLine.Info(Clip.class, format);
        clip = (Clip)AudioSystem.getLine(info);
        clip.open(ais);
        clip.start();
    } catch (Exception e) {
        stopPlay();
        e.printStackTrace();
    }
}
+4
source share
2 answers

The problem is that you need to download the Clip before playing it. Clips are fully loaded into memory before they can be played.

, clip.open() , . Clip, , , clip.start(). , clip.start().

, , ( ) , . , , SourceDataLine, , Clip .

: , , . .

SourceDataLine: , , ( , Clip), , Clip, - . .

LAG: - . . , "" , " ", , , , , .

+7

( JFrame + 80 ), :

  • (, aiff), , ? .
  • Clip . , , ? JVM?
  • - /, ?
+3

Source: https://habr.com/ru/post/1527669/


All Articles