我正在尝试将 .wav 文件加载到内存中,但它一直告诉我该文件不存在。
String filename;
public MyClass(String _filename){
filename = _filename;
}
public void run(){
InputStream in = View.class.getClassLoader().getResourceAsStream("/sounds/"+filename);
File inputFile = new File(in.toString());
if(!inputFile.exists()){
System.err.println("Wave file not found: " + in.toString());
return;
}
}
安慰:
未找到波形文件:java.io.FileInputStream@dd5b524
未找到波形文件:java.io.FileInputStream@570add96
该文件位于包文件夹中。在里面
myPackage/声音/write.wav
编辑:
其实我想加载声音:
InputStream in = this.getClass().getResourceAsStream("sounds/"+filename);
AudioInputStream audioInputStream = null;
try {
audioInputStream = AudioSystem.getAudioInputStream(in);
} catch (UnsupportedAudioFileException e1) {
e1.printStackTrace();
return;
} catch (IOException e1) {
e1.printStackTrace();
return;
}
但是控制台仍然有错误:
javax.sound.sampled.AudioSystem.getAudioInputStream (Unknown Source) com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source) 处的线程“Thread-6”中的异常 java.lang.NullPointerException ) at com.chrissman.threads.AePlayWave.run(AePlayWave.java:47) java.lang.NullPointerException at com.sun.media.sound.SoftMidiAudioFileReader.getAudioInputStream(Unknown Source) at javax.sound.sampled.AudioSystem.getAudioInputStream( Unknown Source) at com.chrissman.threads.AePlayWave.run(AePlayWave.java:47)