在 javafx 项目中播放音乐的方法不会导致播放音乐
method to play music within javafx project does not cause music to play
我创建了一种尝试在 JavaFX 游戏中播放音乐的方法,我放置了一行代码来说明它是否正在播放文件,它确实正确地打印到控制台,说明它正在播放音乐文件,除了我听不到任何音乐播放。
游戏运行良好,音乐文件在项目文件夹中的适当位置,我在启动方法中调用了method music("song.wav");
。所有导入语句都在那里,场景也可以播放。
// called inside start method
music("song.wav");
这是方法
private void music(String musicLocation) {
try {
//take WAV file as input for background music
File musicPath = new File(musicLocation);
//checks if the music file exists in project's path
if (musicPath.exists()) {
System.out.println("The game is playing: " + musicPath);
AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
Clip clip = AudioSystem.getClip();
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
} else {
System.out.println("Can't find file");
}
} catch (UnsupportedAudioFileException ex) {
System.out.println("The specified audio file is not supported.");
ex.printStackTrace();
} catch (LineUnavailableException ex) {
System.out.println("Audio line for playing back is unavailable.");
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("Error playing the audio file.");
ex.printStackTrace();
}
}
}
你不是 associating your AudioInputStream with the Clip。
尝试:
AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
Clip clip = AudioSystem.getClip();
clip.open(audioinput);
clip.start();
我创建了一种尝试在 JavaFX 游戏中播放音乐的方法,我放置了一行代码来说明它是否正在播放文件,它确实正确地打印到控制台,说明它正在播放音乐文件,除了我听不到任何音乐播放。
游戏运行良好,音乐文件在项目文件夹中的适当位置,我在启动方法中调用了method music("song.wav");
。所有导入语句都在那里,场景也可以播放。
// called inside start method
music("song.wav");
这是方法
private void music(String musicLocation) {
try {
//take WAV file as input for background music
File musicPath = new File(musicLocation);
//checks if the music file exists in project's path
if (musicPath.exists()) {
System.out.println("The game is playing: " + musicPath);
AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
Clip clip = AudioSystem.getClip();
clip.start();
clip.loop(Clip.LOOP_CONTINUOUSLY);
} else {
System.out.println("Can't find file");
}
} catch (UnsupportedAudioFileException ex) {
System.out.println("The specified audio file is not supported.");
ex.printStackTrace();
} catch (LineUnavailableException ex) {
System.out.println("Audio line for playing back is unavailable.");
ex.printStackTrace();
} catch (IOException ex) {
System.out.println("Error playing the audio file.");
ex.printStackTrace();
}
}
}
你不是 associating your AudioInputStream with the Clip。
尝试:
AudioInputStream audioInput = AudioSystem.getAudioInputStream(musicPath);
Clip clip = AudioSystem.getClip();
clip.open(audioinput);
clip.start();