我正在尝试为我的 java 游戏添加声音...
我在运行时玩摇摆苏丹:
static String WHOOSH = "res/WHOOSH.WAV";
static String SULTANS = "res/DireStraits_SultansOfSwing.wav";
music(SULTANS, true);
球击中球拍时发出的嗖嗖声
music(WHOOSH, false);
public void music(String path, Boolean loop) {
try {
//will go into file folder and get music file (getResource)
AudioInputStream audio = AudioSystem.getAudioInputStream(GamePanel.class.getResource(path));
Clip clip = AudioSystem.getClip();
clip.open(audio);
clip.start();
if (loop) {
clip.loop(1000);
}
}
catch (Exception e) {
System.out.println("Check: " + path + "\n");
e.printStackTrace();
}
}
问题:
“嗖嗖”总是奏效,但摇摆苏丹却没有。Sultans 给了我这个“不支持的音频文件异常”错误,oracle 文档告诉我
An UnsupportedAudioFileException is an exception indicating that an operation failed because a file did not contain valid data of a recognized file type and format.
错误:
Check: res/DireStraits_SultansOfSwing.wav
javax.sound.sampled.UnsupportedAudioFileException: could not get audio input stream from input URL at javax.sound.sampled.AudioSystem.getAudioInputStream(Unknown Source)
但是你可以从这些照片中看到它们都是.wav文件......
为什么会抛出该错误?是尺寸问题吗?
谢谢!