i've been struggling with trying to add background music to my game, i have put all the code in the main class. any help would be greatly appreciated.
try {
AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(getClass().getResource(musicFilePath));
backgroundMusic = AudioSystem.getClip();
backgroundMusic.open(audioInputStream);
} catch (Exception e) {
e.printStackTrace();
}
}
public void playMusic() {
if (backgroundMusic != null) {
backgroundMusic.loop(Clip.LOOP_CONTINUOUSLY);
}
}
public void stopMusic() {
if (backgroundMusic != null) {
backgroundMusic.stop();
backgroundMusic.close();
}
}
public static void main(String[] args) {
Main game = new Main();
game.loadMusic("test.WAV");
game.playMusic();
// Run your game...
game.stopMusic();
}```