954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

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);
	}

}
corby
Junior Poster
118 posts since Feb 2010
Reputation Points: 6
Solved Threads: 5
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
Code works for me. I changed the .wav reference to a local file:


can you show me?

corby
Junior Poster
118 posts since Feb 2010
Reputation Points: 6
Solved Threads: 5
 

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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
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
118 posts since Feb 2010
Reputation Points: 6
Solved Threads: 5
 

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

NormR1
Posting Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
Sorry, I don't know anything about your IDE.


which one do you use?

corby
Junior Poster
118 posts since Feb 2010
Reputation Points: 6
Solved Threads: 5
 

> 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
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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 Expert
Moderator
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
 
> 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
118 posts since Feb 2010
Reputation Points: 6
Solved Threads: 5
 

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
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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.(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
118 posts since Feb 2010
Reputation Points: 6
Solved Threads: 5
 

Have you tried a different wav file?

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 
Have you tried a different wav file?


no ill try that now

corby
Junior Poster
118 posts since Feb 2010
Reputation Points: 6
Solved Threads: 5
 
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
118 posts since Feb 2010
Reputation Points: 6
Solved Threads: 5
 

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

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 
I wouldn't think so. Perhaps it was just an incompatible file or it was corrupted.


ok thx for your help

corby
Junior Poster
118 posts since Feb 2010
Reputation Points: 6
Solved Threads: 5
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: