We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,716 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

playing backround music in a java program

Hey im trying to run background music when the user clicks the button. below is my coding. when i run the program, it says file cannot be found. any help would be appreciated

import javax.swing.*;
import sun.audio.*;
import java.awt.event.*;
import java.io.*;

public class Sound {
	public static void main(String[] args)
	{
		JFrame frame = new JFrame();
		frame.setSize(200,200);
		JButton button = new JButton("Clcik 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("C:\\test\\ha.wav"));
			MD = BGM.getData();
			loop = new ContinuousAudioDataStream(MD);
		}catch(IOException error){
			System.out.print("file not found");
		}
		
		MGP.start(loop);
	}

}
7
Contributors
21
Replies
1 Year
Discussion Span
2 Weeks Ago
Last Updated
44
Views
Question
Answered
corby
Junior Poster
129 posts since Feb 2010
Reputation Points: 6
Solved Threads: 7
Skill Endorsements: 0

Use error.printStackTrace() instead of just printing "file not found".

Ezzaral
null
Moderator
16,126 posts since May 2007
Reputation Points: 3,294
Solved Threads: 875
Skill Endorsements: 28

Code works for me. I changed the .wav reference to a local file:

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Code works for me. I changed the .wav reference to a local file:

can you show me?

corby
Junior Poster
129 posts since Feb 2010
Reputation Points: 6
Solved Threads: 7
Skill Endorsements: 0

BGM = new AudioStream(new FileInputStream("Audio/mysound.wav"));

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

BGM = new AudioStream(new FileInputStream("Audio/mysound.wav"));

i did that and i still get file not found. i have a bunch of warning signs on a bunch of my lines in the program. is it possibly because of my settings in eclipse?

corby
Junior Poster
129 posts since Feb 2010
Reputation Points: 6
Solved Threads: 7
Skill Endorsements: 0

Sorry, I don't know anything about your IDE.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

Sorry, I don't know anything about your IDE.

which one do you use?

corby
Junior Poster
129 posts since Feb 2010
Reputation Points: 6
Solved Threads: 7
Skill Endorsements: 0

> i have a bunch of warning signs on a bunch of my lines in the program. is it possibly because of my settings in eclipse?
Well, reading them seems to be a good place to start?

Ezzaral
null
Moderator
16,126 posts since May 2007
Reputation Points: 3,294
Solved Threads: 875
Skill Endorsements: 28

I don't use an IDE. I have an enhanced editor with syntax highlighting and commmandlines I can set. Compiles can take < 1 second.

NormR1
Posting Sage
Team Colleague
7,742 posts since Jun 2010
Reputation Points: 1,158
Solved Threads: 793
Skill Endorsements: 16

> i have a bunch of warning signs on a bunch of my lines in the program. is it possibly because of my settings in eclipse?
Well, reading them seems to be a good place to start?

i am not sure how to handle the warnings. this is an example of the warning:
- Access restriction: The type AudioPlayer is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar
- Access restriction: The type AudioPlayer is not accessible due to restriction on required library C:\Program Files\Java\jre6\lib\rt.jar
- Access restriction: The field player from the type AudioPlayer is not accessible due to restriction on required library C:\Program Files\Java
\jre6\lib\rt.jar

corby
Junior Poster
129 posts since Feb 2010
Reputation Points: 6
Solved Threads: 7
Skill Endorsements: 0

Eclipse is obviously not happy that you are directly accessing sun classes. Those are just warnings though and won't prevent you from using them.

Did you change your catch block to print the stack trace of the IOException?

Ezzaral
null
Moderator
16,126 posts since May 2007
Reputation Points: 3,294
Solved Threads: 875
Skill Endorsements: 28

Eclipse is obviously not happy that you are directly accessing sun classes. Those are just warnings though and won't prevent you from using them.

Did you change your catch block to print the stack trace of the IOException?

yea i changed some settings in eclipse to fix the warnings and the stack trace prints this:

java.io.IOException: could not create audio stream from input stream
at sun.audio.AudioStream.<init>(Unknown Source)
at OurGame.Sound.music(Sound.java:30)
at OurGame.Sound$AL.actionPerformed(Sound.java:20)
at javax.swing.AbstractButton.fireActionPerformed(Unknown Source)
at javax.swing.AbstractButton$Handler.actionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.fireActionPerformed(Unknown Source)
at javax.swing.DefaultButtonModel.setPressed(Unknown Source)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(Unknown Source)
at java.awt.Component.processMouseEvent(Unknown Source)
at javax.swing.JComponent.processMouseEvent(Unknown Source)
at java.awt.Component.processEvent(Unknown Source)
at java.awt.Container.processEvent(Unknown Source)
at java.awt.Component.dispatchEventImpl(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source)
at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source)
at java.awt.Container.dispatchEventImpl(Unknown Source)
at java.awt.Window.dispatchEventImpl(Unknown Source)
at java.awt.Component.dispatchEvent(Unknown Source)
at java.awt.EventQueue.dispatchEventImpl(Unknown Source)
at java.awt.EventQueue.access$000(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.awt.EventQueue$1.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.awt.EventQueue$2.run(Unknown Source)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(Unknown Source)
at java.awt.EventQueue.dispatchEvent(Unknown Source)
at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.pumpEvents(Unknown Source)
at java.awt.EventDispatchThread.run(Unknown Source)

corby
Junior Poster
129 posts since Feb 2010
Reputation Points: 6
Solved Threads: 7
Skill Endorsements: 0

Have you tried a different wav file?

Ezzaral
null
Moderator
16,126 posts since May 2007
Reputation Points: 3,294
Solved Threads: 875
Skill Endorsements: 28

Have you tried a different wav file?

no ill try that now

corby
Junior Poster
129 posts since Feb 2010
Reputation Points: 6
Solved Threads: 7
Skill Endorsements: 0

Have you tried a different wav file?

hey i tried a differen wav file and it worked. The orignal file was like 9 kb and this one about 100 kb. So dize the memory size matter?

corby
Junior Poster
129 posts since Feb 2010
Reputation Points: 6
Solved Threads: 7
Skill Endorsements: 0

I wouldn't think so. Perhaps it was just an incompatible file or it was corrupted.

Ezzaral
null
Moderator
16,126 posts since May 2007
Reputation Points: 3,294
Solved Threads: 875
Skill Endorsements: 28

I wouldn't think so. Perhaps it was just an incompatible file or it was corrupted.

ok thx for your help

corby
Junior Poster
129 posts since Feb 2010
Reputation Points: 6
Solved Threads: 7
Skill Endorsements: 0
Question Answered as of 2 Years Ago by Ezzaral and NormR1

I have a problem with this...

import javax.swing.*;
import sun.audio.*;
import java.awt.event.*;
import java.io.*;

public class PikaPlatformer
{

   //-----------------------------------------------------------------
   //  Creates and displays the application frame.
   //-----------------------------------------------------------------
   public static void main (String[] args)
   {
      JFrame frame = new JFrame ("Direction");
      frame.setDefaultCloseOperation (JFrame.EXIT_ON_CLOSE);

      frame.getContentPane().add (new PikaPlatformerPanel());

      frame.pack();
      frame.setVisible(true);
   }
}

How would I insert sound into this I just want music to play in my game. Any suggestions?( this is just the jFrame)

minime010
Newbie Poster
11 posts since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 0

1)Please do not hijack old thread.
2)The answer of how to run it should already be in those posts (using AudioStream class).

Taywin
Posting Maven
2,633 posts since Apr 2010
Reputation Points: 275
Solved Threads: 376
Skill Endorsements: 17

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page generated in 0.1225 seconds using 2.79MB