Results 1 to 3 of 3
  1. #1
    Ballistixz is offline Member
    Join Date
    May 2013
    Rep Power

    Default Need help with adding Background music to my game.

    hi all. im having trouble getting background music to work in a program. to make this post short ill get right to what im having trouble with. first here is the program.

    Java Code:
    import java.awt.event.*;
    import javax.swing.*;
    public class Sound
    	public static void main (String[] args)
    	JFrame frame = new JFrame();
    	frame.setSize (200,200);
    	JButton button = new JButton ("Click me");
    	frame.add (button);
    	button.addActionListener (new AL()); (true);
    	public static class AL implements ActionListener
    		public final void actionPerformed(ActionEvent e)
    	public static void music ()
    		AudioPlayer MGP = AudioPlayer.player;
    		AudioStream BGM;
    		AudioData MD;
    	ContinuousAudioDataStream loop = null;
    	BGM = new AudioStream (new FileInputStream ("E:/java project/Stickerbrush.mp3"));
    	MD = BGM.getData();
    	loop = new ContinuousAudioDataStream(MD);
    	 catch (IOException error){}

    i made this by following a tutorial on youtube that i found and the way it works is that its suppose to play background music whenever i click the button, however when i click on the button no music plays. infact i cant get music to play whatsoever. its wierd because everything compiles successfully (i only get warnings) and the button shows up in a frame, but the music refuses to play. i want to implement this program into a game that me and a project partner is working on. can i get some help to whats wrong or perhaps another tutorial/code that plays background music?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Voorschoten, the Netherlands
    Blog Entries
    Rep Power

    Default Re: Need help with adding Background music to my game.

    In your catch block, add an error.printStackTrace() and see if your music() method completes without going berzerk. The stack trace contains valuable information.

    kind regards,

    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Rep Power

    Default Re: Need help with adding Background music to my game.

    I would also suggest using javax.sound as well.
    Here's the java sound resources site.
    It's full of handy stuff that I have used, chiefly around MIDI in my case, but straight playback is covered in some detail.

    I will say that I didn't think Java ever had an MP3 decoder included with it...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

Similar Threads

  1. Adding a Background image?
    By Mr U in forum Java Applets
    Replies: 3
    Last Post: 10-21-2012, 04:25 PM
  2. Adding image in background
    By nayanjyoti in forum New To Java
    Replies: 1
    Last Post: 04-26-2011, 11:47 AM
  3. Adding JButton to JFrame with background
    By bzknight in forum AWT / Swing
    Replies: 1
    Last Post: 01-19-2011, 06:55 PM
  4. Replies: 1
    Last Post: 01-25-2010, 11:22 PM
  5. adding music to a applet.
    By ramsrocker in forum New To Java
    Replies: 1
    Last Post: 02-14-2009, 04:31 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts