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

    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.*;
    import sun.audio.*;
    import java.io.*;
    
    
    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());
    	frame.show (true);
    
    	}
    	public static class AL implements ActionListener
    	{
    		public final void actionPerformed(ActionEvent e)
    		{
    			music();
    	}
    	}
    
    
    	public static void music ()
    
    	{
    		AudioPlayer MGP = AudioPlayer.player;
    		AudioStream BGM;
    		AudioData MD;
    	ContinuousAudioDataStream loop = null;
    
    	try
    	{
    	BGM = new AudioStream (new FileInputStream ("E:/java project/Stickerbrush.mp3"));
    	MD = BGM.getData();
    	loop = new ContinuousAudioDataStream(MD);
    	 }
    
    	 catch (IOException error){}
    
    
    	 MGP.start(loop);
    	}
    
    }

    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
    Location
    Voorschoten, the Netherlands
    Posts
    12,999
    Blog Entries
    7
    Rep Power
    19

    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,450
    Rep Power
    18

    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.

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
  •