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


public class Sample {

		    public static void main(String s[]) {
       JFrame frame = new JFrame ();
       frame.setSize(200,200);
       JButton justin = new JButton ("Click Here for Welcoming");
       frame.add(justin);
       justin.addActionListener(new AL());
       
		    }
		    public static class AL implements ActionListener{
		    	public final void actionPerformed(ActionEvent e){
		    		music();
		    	}}
		    public static void music(){
		    	AudioPlayer MGP;
		    	AudioStream BGM;
		    	AudioData MD;
		    	ContinouosAudioDataStream loop = null;
		    	
		    	try{
		    		BGM = new AudioStream (new FileInputStream ("aircraft001.wav"));
		    		MD = BGM.getData();
		    		loop = new ContinuousAudioDataStream(MD);
		    	}catch (IOExcepiton error){}
		    	
		    	MPG.start (loop);
		    	}
		    }
}

theres an error with our program, and we dont know why and how, we just tried to follow the instructions of the tutorial!! hope u can help us! thank you!


this is the error that we are getting:


Access restriction: The type AudioPlayer is not accessible due to restriction on required library C:\Program
Files\Java\jre7\lib\rt.jar


and:
Multiple markers at this line
- Access restriction: The type AudioStream is not accessible due to restriction on required
library C:\Program Files\Java\jre7\lib\rt.jar
- Access restriction: The constructor AudioStream(InputStream) is not accessible due to
restriction on required library C:\Program Files\Java\jre7\lib\rt.jar

and also:


MPG cannot be resolved

thank you please help us!

you've called your audioplayer MGP, but you call the start method on MPG, that's why it doesn't find that one.
the other error, I'm not really sure about, I've never used that class, but it looks like you're calling a class from outside the scope where it can be called, is this possible?

what tutorial is it you followed?

you've called your audioplayer MGP, but you call the start method on MPG, that's why it doesn't find that one.
the other error, I'm not really sure about, I've never used that class, but it looks like you're calling a class from outside the scope where it can be called, is this possible?

what tutorial is it you followed?

i saw it on YouTube, this is the name of the video:
Java Tutorial - Add music/sound to java program: background music help - YouTube

well ... I can't go playing vid's here, might take the time when I get home, but I never recommend to use youtube video's as a tutorial for developing.
online tutorials, whether video or not, can be created by anyone who fancies him/herself a programming wizzard, while actually they just found out how to say hello to the world, figure of speech.

I would suggest to take a look at tutorials that are a bit more reliable.

well ... I can't go playing vid's here, might take the time when I get home, but I never recommend to use youtube video's as a tutorial for developing.
online tutorials, whether video or not, can be created by anyone who fancies him/herself a programming wizzard, while actually they just found out how to say hello to the world, figure of speech.

I would suggest to take a look at tutorials that are a bit more reliable.

yes,,,,thats right butm the other tutorials, like this http://docs.oracle.com/javase/tutorial/uiswing/components/formattedtextfield.html
even though we want to apply this on our program, we cant because, the instructions are very hard to understand, and there is no sample codes ..to be followed, we really hope u could help us because , our defense will be on friday :(

http://docs.oracle.com/javase/tutorial/sound/index.html
but you may want to look at this example (the first one).
nothing but core java needed, so you won't have to install any other frameworks or libraries. you'll be limited to play the supported formats, but still, you'll be able to play music. I haven't tried it myself, but it looks solid and easy to look over when you're not familiar with it.

a good tutorial will always contain parts that are hard to understand:
if there's nothing you didn't know before reading it, you don't need the tutorial, and if it only contains 'easy' solutions: chances are that it doesn't contain decent code, but 'work-arounds' that might work, but will have it's cost in efficiƫncy

http://docs.oracle.com/javase/tutorial/sound/index.html
but you may want to look at this example (the first one).
nothing but core java needed, so you won't have to install any other frameworks or libraries. you'll be limited to play the supported formats, but still, you'll be able to play music. I haven't tried it myself, but it looks solid and easy to look over when you're not familiar with it.

a good tutorial will always contain parts that are hard to understand:
if there's nothing you didn't know before reading it, you don't need the tutorial, and if it only contains 'easy' solutions: chances are that it doesn't contain decent code, but 'work-arounds' that might work, but will have it's cost in efficiƫncy

true, but what i cant understand with my teacher is that she wants us to add something that we are not familiar with, and thats why, even though we really want to understand something, we keep on asking ourselves why did this happen, or where this came from,

:(


ill try to look for more tutorials, well, this sound, is just something to add on our system, ill try to focus on Jframes :)

well ...
that's basically like every other teacher. it's not just her job to teach you how to program, but also to make sure you are capable to develop software without a teacher standing by your side. when you come into a work environment, chances are they'll be working with frameworks you've never even heard of, but that doesn't mean you'll have all the time of the world to get acquainted with them.

looking through api's, documentation, and searching for new and better solutions are a part of your job as developer, she's just preparing you for it :).

actually, adding sounds can be very easy, but you'll need to know exactly what you want:

an .mp3 player which shifts automatically to the next song once the song is finished without interaction of the user
just a 'click' sound played when you click a button,
...

lots of possibilities out there :)

well ...
that's basically like every other teacher. it's not just her job to teach you how to program, but also to make sure you are capable to develop software without a teacher standing by your side. when you come into a work environment, chances are they'll be working with frameworks you've never even heard of, but that doesn't mean you'll have all the time of the world to get acquainted with them.

looking through api's, documentation, and searching for new and better solutions are a part of your job as developer, she's just preparing you for it :).

actually, adding sounds can be very easy, but you'll need to know exactly what you want:

an .mp3 player which shifts automatically to the next song once the song is finished without interaction of the user
just a 'click' sound played when you click a button,
...

lots of possibilities out there :)

my purpose is to add some sound effects on my program (the payroll system) thats what i want to do, and yes, thats true that my teacher should not be always by my side, because she needs to train us on our work (if ever that i will be a programmer someday!)

:) sir, can u give me a video tutorial of Jtextfields??
i find it easier to learn using videos, ill use that Jtextfields on my Payroll System


we will be presenting our system on Friday and, although i know that it will be very impossible for me to learn all of the other things that is concerned with the Jframe, i still want learn it, im really eager to know and i want not just to pass our subject but also to be one of the high scorers ..thank you sir!

what do you need to know of JTextFields?
best way to find out what you can do with them is the api's, not some video's.
video's are good when they deal about a conference, not that much about explaining basic tasks (imho).

if you just say what you need to know, maybe I can help explaining, and showing you where you can find information about it in the api's.

what do you need to know of JTextFields?
best way to find out what you can do with them is the api's, not some video's.
video's are good when they deal about a conference, not that much about explaining basic tasks (imho).

if you just say what you need to know, maybe I can help explaining, and showing you where you can find information about it in the api's.

sir, if u will be able to run our program, u will see that it will require u to enter some basic information, such as name, address, and so on, but i dont want to just use JOptionPane, bcoz, everytime, it will ask for something , a new window opens up, i dont want that, i want it to ask all of it in just one window and input it also in just one window :)

sir, if u will be able to run our program, u will see that it will require u to enter some basic information, such as name, address, and so on, but i dont want to just use JOptionPane, bcoz, everytime, it will ask for something , a new window opens up, i dont want that, i want it to ask all of it in just one window and input it also in just one window :)

well if thats your plan then all good and well, to implement it, lets go back to logic-what was built on- how in your head would you separate a string containing various sets of data? most importantly you'd need a separating character that will let you know you have reached the end of the first input, thus the beginning of the next. So an example:

Scanner sc=new Scanner(System.in);
String s=sc.nextLine();//will read in multiple input separated by ',' i.e. David,Alan
String[] sarray=s.split(",");
System.out.println(sarray[0]);
System.out.println(sarray[1]);

[edit] even though i used the scanner class, i hope you get the logic behind it to convert this to Optionpanes

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


public class Sample {

		    public static void main(String s[]) {
       JFrame frame = new JFrame ();
       frame.setSize(200,200);
       JButton justin = new JButton ("Click Here for Welcoming");
       frame.add(justin);
       justin.addActionListener(new AL());
       
		    }
		    public static class AL implements ActionListener{
		    	public final void actionPerformed(ActionEvent e){
		    		music();
		    	}}
		    public static void music(){
		    	AudioPlayer MGP;
		    	AudioStream BGM;
		    	AudioData MD;
		    	ContinouosAudioDataStream loop = null;
		    	
		    	try{
		    		BGM = new AudioStream (new FileInputStream ("aircraft001.wav"));
		    		MD = BGM.getData();
		    		loop = new ContinuousAudioDataStream(MD);
		    	}catch (IOExcepiton error){}
		    	
		    	MPG.start (loop);
		    	}
		    }
}

theres an error with our program, and we dont know why and how, we just tried to follow the instructions of the tutorial!! hope u can help us! thank you!


this is the error that we are getting:


Access restriction: The type AudioPlayer is not accessible due to restriction on required library C:\Program
Files\Java\jre7\lib\rt.jar


and:
Multiple markers at this line
- Access restriction: The type AudioStream is not accessible due to restriction on required
library C:\Program Files\Java\jre7\lib\rt.jar
- Access restriction: The constructor AudioStream(InputStream) is not accessible due to
restriction on required library C:\Program Files\Java\jre7\lib\rt.jar

and also:


MPG cannot be resolved

thank you please help us!

Try the following code:

Imports required:

import javax.media.format.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;



          try{      
		AudioInputStream stream        = AudioSystem.getAudioInputStream(new File("POWER_7.wav"));      
		AudioFormat format = stream.getFormat();      
		DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat());      
		Clip clip = (Clip) AudioSystem.getLine(info);         
		 clip.open(stream);      
		 clip.start();
	} catch (Exception H)
	{      
	 	H.printStackTrace();    
	}

Try the following code:

Imports required:

import javax.media.format.AudioFormat;
import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import javax.sound.sampled.DataLine;



          try{      
		AudioInputStream stream        = AudioSystem.getAudioInputStream(new File("POWER_7.wav"));      
		AudioFormat format = stream.getFormat();      
		DataLine.Info info = new DataLine.Info(Clip.class, stream.getFormat());      
		Clip clip = (Clip) AudioSystem.getLine(info);         
		 clip.open(stream);      
		 clip.start();
	} catch (Exception H)
	{      
	 	H.printStackTrace();    
	}

sir its not working !!! :(

what does the stacktrace say? do you have the .wav file (if you have one) on the right location?

how did you add the above code to yours?

just found a link that might be of some use:
http://www.oracle.com/technetwork/java/index-139508.html

sir its not working !!! :(

First of all is your sound file in your programs directory.
Second what format sound is your file?

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.