我们需要一个类
AePlayWave
package tankgame6;
/**
* Create By 刘鸿涛
* 2022/2/5 18:06
*/
import javax.sound.sampled.*;
import java.io.File;
import java.io.IOException;
//public class AePlayWave {
// public static void main(String[] args){
// AePlayWave apw = new AePlayWave("D:\\start.wav");
// apw.start();
// }
//}
class AePlayWave extends Thread{
private String filename;
public AePlayWave(String wavefile){
filename = wavefile;
}
public void run(){
File SoundFile = new File(filename);
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(SoundFile);
} catch (Exception e) {
e.printStackTrace();
return;
}
AudioFormat Format = audioInputStream.getFormat();
SourceDataLine sdl = null;
DataLine.Info info = new DataLine.Info(SourceDataLine.class,Format);
try {
sdl = (SourceDataLine)AudioSystem.getLine(info);
sdl.open(Format);
} catch (Exception e) {
e.printStackTrace();
return;
}
sdl.start();
int nBytesRead = 0;
//缓冲
byte[] abData = new byte [1024];
try {
while(nBytesRead!=-1) {
nBytesRead = audioInputStream.read(abData, 0, abData.length);
if (nBytesRead >= 0) {
sdl.write(abData, 0, nBytesRead);
}
}
} catch (Exception e) {
e.printStackTrace();
return;
} finally {
sdl.drain();
sdl.close();
}
}
}
MyPanel类中新建AePlayWave对象,通过里面的run方法,直接调用即可
new AePlayWave("src\\start.wav").start();
很抱歉,参考资料给出的是一个与问题无关的Java代码示例,没有办法帮您解决问题。请提供您的Java代码和出错信息,我们会尽可能帮助您解决问题。
你这代码都有问题呀,都return了,后边的代码还有什么用呢