Javas音频剪辑在频繁播放嘟嘟声时出现问题

Javas音频剪辑在频繁播放嘟嘟声时出现问题,java,audio,clip,Java,Audio,Clip,我想在GUI触发的操作成功或出错时播放短的蜂鸣音(WAV文件) 我偶然发现,这似乎奏效了。 以下是我使用的基本代码: clip.stop(); clip.setFramePosition(0); clip.start(); 这是在单击按钮触发数据库操作后执行的。在成功和出错时,将播放两个不同的预加载剪辑 但在制作机器上(一台运行Kubuntu 10.4的旧PC),经过一段时间(大约400多次执行或2-4小时),剪辑拒绝播放。 停止方法大约需要3秒钟才能终止,并且以下启动操作不会播放任何声音。随

我想在GUI触发的操作成功或出错时播放短的蜂鸣音(WAV文件)

我偶然发现,这似乎奏效了。 以下是我使用的基本代码:

clip.stop();
clip.setFramePosition(0);
clip.start();
这是在单击按钮触发数据库操作后执行的。在成功和出错时,将播放两个不同的预加载剪辑

但在制作机器上(一台运行Kubuntu 10.4的旧PC),经过一段时间(大约400多次执行或2-4小时),剪辑拒绝播放。 停止方法大约需要3秒钟才能终止,并且以下启动操作不会播放任何声音。随后每次调用代码都会失败,不会引发异常或任何其他反馈。 唯一能解决这个问题的是重新启动整个应用程序

我的问题是:
有什么解决办法吗?其他人也有同样的问题吗?或者是否有另一个框架可以用来播放至少两种不同的声音(Toolkit.beep()只能播放一种声音)。

不要害怕只重新创建对象,开销很低。与其重置剪辑,不如尝试创建新的剪辑。您可以缓存文件,这将是一个有用的优化。重复使用剪辑对象是不正确的

或者您可以尝试另一种实现[不受限制]

这是谷歌搜索“java播放wav文件”的最高结果:

它将事情简化为一次调用:

new AePlayWave("test.wav").start();
只需将此类添加到您的代码库:

import java.io.File; 
import java.io.IOException; 
import javax.sound.sampled.AudioFormat; 
import javax.sound.sampled.AudioInputStream; 
import javax.sound.sampled.AudioSystem; 
import javax.sound.sampled.DataLine; 
import javax.sound.sampled.FloatControl; 
import javax.sound.sampled.LineUnavailableException; 
import javax.sound.sampled.SourceDataLine; 
import javax.sound.sampled.UnsupportedAudioFileException; 

public class AePlayWave extends Thread { 

    private String filename;

    private Position curPosition;

    private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb 

    enum Position { 
        LEFT, RIGHT, NORMAL
    };

    public AePlayWave(String wavfile) { 
        filename = wavfile;
        curPosition = Position.NORMAL;
    } 

    public AePlayWave(String wavfile, Position p) { 
        filename = wavfile;
        curPosition = p;
    } 

    public void run() { 

        File soundFile = new File(filename);
        if (!soundFile.exists()) { 
            System.err.println("Wave file not found: " + filename);
            return;
        } 

        AudioInputStream audioInputStream = null;
        try { 
            audioInputStream = AudioSystem.getAudioInputStream(soundFile);
        } catch (UnsupportedAudioFileException e1) { 
            e1.printStackTrace();
            return;
        } catch (IOException e1) { 
            e1.printStackTrace();
            return;
        } 

        AudioFormat format = audioInputStream.getFormat();
        SourceDataLine auline = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

        try { 
            auline = (SourceDataLine) AudioSystem.getLine(info);
            auline.open(format);
        } catch (LineUnavailableException e) { 
            e.printStackTrace();
            return;
        } catch (Exception e) { 
            e.printStackTrace();
            return;
        } 

        if (auline.isControlSupported(FloatControl.Type.PAN)) { 
            FloatControl pan = (FloatControl) auline
                    .getControl(FloatControl.Type.PAN);
            if (curPosition == Position.RIGHT) 
                pan.setValue(1.0f);
            else if (curPosition == Position.LEFT) 
                pan.setValue(-1.0f);
        } 

        auline.start();
        int nBytesRead = 0;
        byte[] abData = new byte[EXTERNAL_BUFFER_SIZE];

        try { 
            while (nBytesRead != -1) { 
                nBytesRead = audioInputStream.read(abData, 0, abData.length);
                if (nBytesRead >= 0) 
                    auline.write(abData, 0, nBytesRead);
            } 
        } catch (IOException e) { 
            e.printStackTrace();
            return;
        } finally { 
            auline.drain();
            auline.close();
        } 

    } 
} 
那么我是如何修复它的:

我基本上遵循了Charles Goodwin的提示,但我更改了AePlayWave类以实现runnable,并将其与线程池一起使用(以避免始终启动新线程的开销)。此外,我使用URL而不是文件来使用打包JAR文件中的资源。AePlayWave对象是在完成设置(选择文件)或更改设置时创建的。我希望应用程序播放的每个声音都有一个实例。然后,事件的侦听器方法触发池为该事件运行特定的AePlayWave实例。其余的基本相同

只有两个不方便的问题:

1.)在较弱的机器上,WAV的结尾并不总是播放。当声音很短时(如100毫秒的蜂鸣声),可能会导致根本没有声音播放!这就是为什么我在我想演奏的每个声音的结尾加上500毫秒的静默。这是一个解决办法,但它有帮助,而且目前看来是最好、最稳定的方法

2.)如果播放一个以上的声音(因为重复速度非常快),声音会重叠,您会听到音调和音量的变化。这在我的情况下是可以的,但对于其他用途来说可能会很烦人

它已经在生产系统上运行。如果有任何错误报告给我,我会编辑这篇文章,让你保持最新

下面是(基本上简化的)源代码:

import java.io.IOException;
import java.net.URL;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;    
import javax.sound.sampled.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.DataLine;
import javax.sound.sampled.LineUnavailableException;
import javax.sound.sampled.SourceDataLine;
import javax.sound.sampled.UnsupportedAudioFileException;

public class AudibleListener implements SomeListener {

private Runnable successRunner;
private Runnable failRunner;

ExecutorService pool = Executors.newCachedThreadPool();

/**
 * Call this after initialization and after every change in your config at runtime.
 */
public void reloadSettings() {
    // put your configuration here
    this.successRunner = new WavePlayer(this.getClass().getResource("success.wav"));
    this.failRunner = new WavePlayer(this.getClass().getResource("fail.wav"));
}

/**
 * Call this to savely shutdown the thread pool.
 */
public void shutdown() {
    this.pool.shutdown();
}

/**
 * Listener method called on success. 
 */
public void eventSuccess() {
    this.pool.execute(this.successRunner);
}

/**
 * Listener method called on fail. 
 */
public void eventFailed() {
    this.pool.execute(this.failRunner);
}

private class WavePlayer implements Runnable {

    private final int EXTERNAL_BUFFER_SIZE = 524288; // 128Kb 

    private URL soundFile;

    public WavePlayer(URL soundFile) {
        this.soundFile = soundFile;
    }

    @Override
    public void run() {

        try {
            // check if the URL is still accessible!
            this.soundFile.openConnection().connect();
            this.soundFile.openStream().close();
        } catch (Exception e) {
            return;
        }

        AudioInputStream audioInputStream = null;
        try {
            audioInputStream = AudioSystem
                    .getAudioInputStream(this.soundFile);
        } catch (UnsupportedAudioFileException e) {
            return;
        } catch (IOException e) {
            return;
        }

        AudioFormat format = audioInputStream.getFormat();
        SourceDataLine auline = null;
        DataLine.Info info = new DataLine.Info(SourceDataLine.class, format);

        try {
            auline = (SourceDataLine) AudioSystem.getLine(info);
            auline.open(format);
        } catch (LineUnavailableException e) {
            return;
        } catch (Exception e) {
            return;
        }

        auline.start();
        int nBytesRead = 0;
        byte[] abData = new byte[this.EXTERNAL_BUFFER_SIZE];

        try {
            while (nBytesRead != -1) {
                nBytesRead = audioInputStream
                        .read(abData, 0, abData.length);
                if (nBytesRead >= 0) {
                    auline.write(abData, 0, nBytesRead);
                }
            }
        } catch (IOException e) {
            return;
        } finally {
            auline.drain();
            auline.close();
        }
    }
}
}
干杯,感谢所有的帮助

p

更新:


这是现在运行了过去72小时没有任何错误!看来我们成功了

“(Toolkit.beep()只能播放一种声音)。“是的,但它可以多次播放,并且在两次嘟嘟声之间有不同的延迟!”看看你是否有同样的行为。关于嘟嘟声:是的。如果我玩得够多的话,听起来就像是Bogey上校的游行。。。我尝试过这个选择,但这将是我最后的选择,因为我不是一个论坛。这里的规则是编辑您的问题/答案以添加信息。另外,上面的帖子也是一个问题。下面的帖子是答案。每个用户只有一个答案。我建议你读一下。谢谢你的提示。我无法发表评论,因为我没有注册,正在另一台计算机上工作。我将相应地移动/删除我的更改。这是一个很大的帮助。非常感谢你。