java为游戏添加⾳效,如何在Java游戏播放声⾳?
I'm successfully playing sounds in a Java (1.5) applet game using the following code:
// get an available clip to play it
navicat注册机使用教程Clip clip = null;
for (Clip clipTemp : players) {
if (!clipTemp.isOpen()) {
clip = clipTemp;
break;
}
}
if (clip == null) {
/
/ no available player found, don't play
return;计算机网课视频教程
}
clip.open(audioFormat, audioByteData, 0, audioByteData.length);
strlen是关键字吗clip.start();
(Players are a list of clips that I open at the start with the aim to reduce latency, a line listener closes the line when the stop event is retrieved.)
The problem I'm facing is intermittent delays of upto 1 second when playing a sound. This is pretty poor.
Is there any way to improve this? Are SourceDataLines worth considering?
解决⽅案
The Java Applet is streaming your clip whenever you want to play it, which is why you are getting a d
elay as the sound file hasn't been loaded into memory yet.
It's been awhile since I have done Java applet programming, but I do remember that I used to pre-load all my clips and then subsequent calls to play would not re-open the files.
Here is some code from one of my old projects
linux被黑Clip shoot;
private loadShootWav()
{
AudioInputStream sample;
sample = Class().getResource("shoot.wav"));
shoot = Clip();
shoot.open(sample);
}
手机反编译xml
public void playShootSFX() {
shoot.stop();
shoot.setFramePosition(0); shoot.start();
}
>java手机游戏下载
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论