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

}

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

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

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

can you show me?

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

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?

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

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

which one do you use?

> 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 don't use an IDE. I have an enhanced editor with syntax highlighting and commmandlines I can set. Compiles can take < 1 second.

> 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

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?

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)

Have you tried a different wav file?

Have you tried a different wav file?

no ill try that now

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?

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

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

ok thx for your help

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)

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

it keeps saying file not found, even though its in the same folder. Any suggestions for me?

  1. start a new thread - don't hijack an old one
  2. show your code
  3. show the actual error message
  4. ... " it's in the same folder" ... doesn't automatically means your application is checking that location.

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

there is a button in project. after i click that i want play a song which is my computer ?

Wazan:

Do not hijack old forum threads by posting a new question as a reply to an old one http://www.daniweb.com/community/rules

Start your own new thread if you have a question.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.