可靠玩Java中的短声 [英] Reliably playing a short sound in Java

查看:110
本文介绍了可靠玩Java中的短声的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我tyring写一些Java code,它基本上只是扮演一个短的.wav文件 - 与短我的意思是几分之一秒。 (我使用的文件是在/usr/share/sounds/generic.wav对于那些你使用Ubuntu。)

I am tyring to write some Java code that basically just plays a short .wav file - with 'short' I mean a fraction of a second. (The file I use is at /usr/share/sounds/generic.wav for those of you using Ubuntu.)

问题是,我似乎无法弄清楚如何发挥这一样本的可靠的,也就是说,在我所有的努力,我可以让我的程序4播放声音,满分5次左右,但从来没有100%。

The problem is, I can't seem to figure out how to play that sample reliably, i.e., in all my attempts, I can get my program to play the sound in 4 out of 5 times or so, but never 100%.

这是什么工作最好,只要一个独立的程序:

This is what has worked best so far as a stand-alone program:

File soundFile = new File("/usr/share/sounds/generic.wav");
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(soundFile);
clip.open(inputStream);
clip.start(); 

(注意:code甚至不叫clip.stop()),但即使有那一个,如果我运行它几次在一排,迟早会出现没有任何一个运行声音正在播放,但没有任何例外

(Note that the code doesn't even call clip.stop()) But even with that one, if I run it a couple of times in a row, sooner or later there will be a run without any sound being played, but no Exceptions either.

变奏曲我试过:

1)加载音频文件转换成字节数组并传递,为clip.open

1) Loading the audio file into a byte array and passing that to clip.open

2)安装一个LineListener剪辑等待STOP事件

2) Attaching a LineListener to the clip to wait for STOP events

再加上一对夫妇随机试奏的,但到目前为止,我还没有成功地创造code,工程的每次

plus a couple of random try-outs, but so far I haven't managed to create code that works every time.

我也知道下面的bug:的http://错误。 sun.com/bugdatabase/view_bug.do?bug_id=4434125 但我使用Java 1.6,该报告称,事情应该是罚款与Java 1.5或更高版本。

I'm also aware of the following bug: http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4434125 but I'm using Java 1.6 and the report claims that things should be fine with Java 1.5 or later.

任何想法?难道是pulseaudio的?

Any ideas? Is it PulseAudio?

推荐答案

我现在怀疑我的测试程序失败的原因是一个时间问题。无论是我试图打短声之前对样品进行满载或程序过快终止。这样做的原因怀疑是,如果我改变上述code稍微像这样:

I suspect now that the reason my test program failed was a timing issue. Either I attempted playing the short sound before the samples were fully loaded, or the program terminated too quickly. The reason for this suspicion is that if I change the above code slightly like so:

File soundFile = new File("/usr/share/sounds/generic.wav");
Clip clip = AudioSystem.getClip();
AudioInputStream inputStream = AudioSystem.getAudioInputStream(soundFile);
clip.open(inputStream);

while (System.in.read() == '\n') {
    clip.stop();
    clip.setFramePosition(0);
    clip.start();
}

那么短的声音被正确每次我打出场时间<大骨节病>输入

这篇关于可靠玩Java中的短声的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆