gpt4 book ai didi

java - 如何在java中播放.wav文件而不出现游戏延迟

转载 作者:行者123 更新时间:2023-12-01 14:13:08 25 4
gpt4 key购买 nike

我有一个游戏,我想在其中播放音频。我想要做的是我应该能够使用路径名参数声明一个 ClipPlayer 对象。然后构造函数应该能够加载声音,然后我可以使用该对象进行调用,这样它就不会滞后。这是我当前用于声音的代码:

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;

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 ClipPlayer {

AudioInputStream in;

AudioFormat decodedFormat;

AudioInputStream din;

AudioFormat baseFormat;

SourceDataLine line;

private boolean loop;

private BufferedInputStream stream;

// private ByteArrayInputStream stream;

/**
* recreate the stream
*
*/
public void reset() {
try {
stream.reset();
in = AudioSystem.getAudioInputStream(stream);
din = AudioSystem.getAudioInputStream(decodedFormat, in);
line = getLine(decodedFormat);

} catch (Exception e) {
e.printStackTrace();
}
}

public void close() {
try {
line.close();
din.close();
in.close();
} catch (IOException e) {
}
}

ClipPlayer(String filename, boolean loop) {
this(filename);
this.loop = loop;
}

ClipPlayer(String filename) {
this.loop = false;
try {
InputStream raw = Object.class.getResourceAsStream(filename);
stream = new BufferedInputStream(raw);

// ByteArrayOutputStream out = new ByteArrayOutputStream();
// byte[] buffer = new byte[1024];
// int read = raw.read(buffer);
// while( read > 0 ) {
// out.write(buffer, 0, read);
// read = raw.read(buffer);
// }
// stream = new ByteArrayInputStream(out.toByteArray());

in = AudioSystem.getAudioInputStream(stream);
din = null;

if (in != null) {
baseFormat = in.getFormat();

decodedFormat = new AudioFormat(
AudioFormat.Encoding.PCM_SIGNED, baseFormat
.getSampleRate(), 16, baseFormat.getChannels(),
baseFormat.getChannels() * 2, baseFormat
.getSampleRate(), false);

din = AudioSystem.getAudioInputStream(decodedFormat, in);
line = getLine(decodedFormat);
}
} catch (UnsupportedAudioFileException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (LineUnavailableException e) {
e.printStackTrace();
}
}

private SourceDataLine getLine(AudioFormat audioFormat)
throws LineUnavailableException {
SourceDataLine res = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class,
audioFormat);
res = (SourceDataLine) AudioSystem.getLine(info);
res.open(audioFormat);
return res;
}

public void play() {

try {
boolean firstTime = true;
while (firstTime || loop) {

firstTime = false;
byte[] data = new byte[4096];

if (line != null) {

line.start();
int nBytesRead = 0;

while (nBytesRead != -1) {
nBytesRead = din.read(data, 0, data.length);
if (nBytesRead != -1)
line.write(data, 0, nBytesRead);
}

line.drain();
line.stop();
line.close();

reset();
}
}
} catch (IOException e) {
e.printStackTrace();
}
}

}

最佳答案

将声音声明、加载和缓存为类属性。然后,当需要聆听它们时,在 Clip 中播放它们。

关于java - 如何在java中播放.wav文件而不出现游戏延迟,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18333588/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com