#help with adding music to game!!!

8 messages · Page 1 of 1 (latest)

nova sierra
#

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();
    }```
zenith ravenBOT
#

This post has been reserved for your question.

Hey @nova sierra! Please use /close or the Close Post button above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.

TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.

native ridge
#

what's the problem?

nova sierra
#

the music wont play

#

when i start the game the background music should paly

#

but nothing happens

dim mural
#

dude, javafx has music support