gpt4 book ai didi

java - 我的游戏中出现 .wav、.au 声音后出现极度延迟?

转载 作者:行者123 更新时间:2023-12-02 06:51:13 26 4
gpt4 key购买 nike

我尝试在网上搜索资源非常有限。在操作执行方法中:

actionPerformed(){
---------------
new Sound();}

在声音类中

 public Sound(){
try {
String filePath="path\\file.wav";
File path= new File(filePath);
AudioInputStream stream;
AudioFormat format;
DataLine.Info info;
Clip clip;

stream = AudioSystem.getAudioInputStream(path);
format = stream.getFormat();//becomes formatted
info = new DataLine.Info(Clip.class, format);// info
clip = (Clip) AudioSystem.getLine(info);// casting for clip
clip.open(stream); // clip opens stream with path
clip.start();
}
catch (Exception e) {
System.out.println("errors");
}

}

谢谢。

最佳答案

根据 .start() 的 Javadoc方法:

Allows a line to engage in data I/O. If invoked on a line that is already running, this method does nothing. Unless the data in the buffer has been flushed, the line resumes I/O starting with the first frame that was unprocessed at the time the line was stopped. When audio capture or playback starts, a START event is generated.

没有提及以异步方式播放音频。这意味着您的声音将由调用 start 方法的线程处理,由于您在事件处理程序中调用它,因此最终意味着您的事件调度线程正在处理音频播放。

该线程还负责在屏幕上绘制内容,因此在其上运行的任何其他内容(在您的情况下为音频)都会对渲染产生负面影响。

您可以使用如下代码(取自 here )使您的音频在单独的线程上播放。这应该可以解决您的滞后问题。

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

}
}

关于java - 我的游戏中出现 .wav、.au 声音后出现极度延迟?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18009542/

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