剪辑播放WAV文件与Java的坏滞后

剪辑播放WAV文件与Java的坏滞后

本文介绍了剪辑播放WAV文件与Java的坏滞后的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个code读取WAV文件(大小约为80 MB),并播放。问题是,播放声音严重(极端滞后)。能否请你告诉我什么问题?

下面是我的code:
(我称之为 doPlay 函数一个JFrame构造函数中)

 私人无效doPlay(最终字符串路径){
    尝试{
        stopPlay();
        InputStream为=新的FileInputStream(路径);
        InputStream的bufferedIn =新的BufferedInputStream(是);
        AIS的AudioInputStream = AudioSystem.getAudioInputStream(bufferedIn);
        AudioFormat的格式= ais.getFormat();
        //这是格式的价值。
        // PCM_SIGNED 44100.0赫兹,16位立体声,4个字节/帧,小尾数
        DataLine.Info信息=新DataLine.Info(Clip.class,格式);
        夹=(CLIP)AudioSystem.getLine(信息);
        clip.open(AIS);
        clip.start();
    }赶上(例外五){
        stopPlay();
        e.printStackTrace();
    }
}


解决方案

的问题是,你需要加载剪辑的的打它。剪辑加载到内存中,他们可以播放之前完全。

之前,它是时间播放的剪辑

在换句话说,一切都交给clip.open()应该会出现好。当您准备播放的剪辑,你应该使用的唯一的命令是clip.start()。要重播片段,设置其光标位置返回到开始和呼叫clip.start()。

如果剪辑足够短,你可以逃脱打开这些低效率的编码习惯在同一时间,当您播放它们(加载它们)。如果你真的想从一个文件,而不是从播放内存中,使用的SourceDataLine,它会开始比片段将快得多了较大的文件。

剪辑:必须加载到内存中才能播放,加载后,小青用最小的CPU。专为小型和重用的声音文件。

的SourceDataLine:从文件的位置戏剧,立即启动,消耗很少的内存(比片段少得多),但使用比,因为该文件读取的片段稍微更多的CPU。最适合较大和单播放音频。

LAG的另一个原因:第一次声音文件被调用时,它运行因为编译code执行的慢一点。重用,声音code放入内存中,并以最少的滞后执行。因此,有时我在节目开始发挥无声的声音黄金泵,这样,当需要的第一个声音被听到的戏剧,它起着用较少的滞后。

I've written a code that read a WAV file (size is about 80 mb) and plays that. The problem is that sound plays badly (extreme lags). Can you please tell me what's the problem?

Here's my code:(I call the doPlay function inside a 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();
    }
}
解决方案

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

In other words, everything up to clip.open() should occur well before it is time to play the clip. When you are ready to play the Clip, the only command you should use is clip.start(). To replay the clip, set its cursor position back to the start and call clip.start().

If a Clip is short enough, you can get away with the inefficient coding practice of opening them (loading them) at the same time as when you play them. If you really want to play from a file instead of from memory, use SourceDataLine, and it will start much faster than the Clip will for larger files.

Clip: has to be loaded into memory before it can play, once loaded, it plays with minimal cpu. Designed for small, and reused sound files.

SourceDataLine: plays from file location, starts immediately, consumes very little memory (much less than Clip) but uses slightly more cpu than Clip because of the file reading. Best for larger and single-play audio.

Another source of LAG: first time a sound file is called, it runs a little slower because of executing from compiled code. With reuse, the sound code is put into memory and executes with minimal lag. Thus, sometimes I play a "silent" sound at the start of a program to "prime the pump" so that when the first sound that needs to be heard plays, it plays with less lag.

这篇关于剪辑播放WAV文件与Java的坏滞后的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 01:01