我现在正在开发一个 Java 游戏项目,并且正在尝试导入音乐。但是,我似乎无法让它工作:
import java.io.File;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
public class Music {
public static void playSound(String name) {
try {
System.out.println("music is now playing");
File file = new File("audio/" + name + ".wav");
AudioInputStream stream = AudioSystem.getAudioInputStream(file);
Clip clip = AudioSystem.getClip();
clip.open(stream);
clip.start();
// sleep to allow enough time for the clip to play
Thread.sleep(10);
stream.close();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
以上是我的music.java类的代码,然后在我的主game.java类中调用。
public static void main(String[] args) {
// instantiate this object
Music.playSound("ahem");
Game game = new Game();
} // main
但我的输出返回
music is now playing
audio/ahem.wav (No such file or directory)
我相当确定我的 .wav 文件放置在正确的位置(在我的“bin”文件夹内的“audio”文件夹中,所以我不确定发生了什么。