2

我想在 GUI 触发操作的成功和错误时播放短哔声(WAV 文件)。

我遇到了javax.sound.sampled.Clip,它似乎有效。这是我使用的基本代码:

clip.stop();
clip.setFramePosition(0);
clip.start();

这是在单击按钮触发数据库操作后执行的。在成功和错误时播放两个不同的预加载剪辑。

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

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

4

2 回答 2

2

不要害怕重新创建对象,开销很低。不要重置剪辑,而是尝试创建新剪辑。您可以缓存文件,这将是一个有用的优化。不能重用剪辑对象。

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

这是 Google 中“java play wav files”的最高结果:

http://www.anyexample.com/programming/java/java_play_wav_sound_file.xml

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

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();
        } 

    } 
} 
于 2011-07-08T23:42:47.137 回答
1

那么我如何修复它:

我基本上遵循了 Charles Goodwin 的提示,但我更改了 AePlayWave 类以实现可运行并将其与线程池一起使用(以避免一直启动新线程的开销)。此外,我使用 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 小时,没有任何错误!看起来我们成功了!

于 2011-07-12T15:25:09.253 回答