Jump to content

How do I add BGMs on Java? I'm currently building a GUI-based Magic Square simulation and I'm planning to put it a background music on that. I tried two "test" codes but both didn't work. I put my music file on my source folder and a copy in D:/ root folder.

public static void music(){     /*AudioPlayer myBackgroundPlayer = AudioPlayer.player;     ContinuousAudioDataStream myLoop = null;          try {           AudioInputStream myBackgroundMusic = AudioSystem.getAudioInputStream(new File ("TFC.mp3"));           Clip sound = AudioSystem.getClip();           sound.open(myBackgroundMusic);           sound.start();              }     catch(Exception error){         System.out.println(error);     }     myBackgroundPlayer.start(myLoop);     AudioInputStream audioIn = AudioSystem.getAudioInputStream(MyClazz.class.getResource("TFC.mp3"));    Clip clip = AudioSystem.getClip();    clip.open(audioIn);    clip.start(); */                AudioPlayer BGP = AudioPlayer.player;        AudioStream BGM;        AudioData MD;        ContinuousAudioDataStream loop = null;        try{            BGM = new AudioStream (new FileInputStream ("TFC.mp3"));            MD = BGM.getData();            loop = new ContinuousAudioDataStream (MD);        }        catch (IOException error){            System.out.println(error);        }        BGP.start(loop); }

For my main, I instantiated an object to call it.

 

 

Spoiler

 

Link to comment
https://linustechtips.com/topic/369632-background-music-for-java/
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×