I have one Button on my Applet and I want that when I click on Button audio file MySound.wav will play. I am using AWT for creating applet.
Printable View
I have one Button on my Applet and I want that when I click on Button audio file MySound.wav will play. I am using AWT for creating applet.
I'm not sure what your question is. Do you need to know the classes and methods for playing a sound? You would use Applet.getAudioClip() to load the sound and AudioClip.play() to play it.
Norm
Thank you for your anwser i know that for playing sound i can use AudioClip class but i don't know how. i write this prog. but it didn't work.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="MySound" width=200 height=200>
</applet>
*/
public class MySound extends Applet {
AudioClip audioClip;
public void init()
audioClip = Applet.getAudioClip(getCodeBase(),"gradually.wav");
audioClip.play();
}
}
You didn't say what version of the JVM you were using. I think that .wav files work on Java v1.2. If you're running it in a browser without the plugin, it won't work. Most browsers are at v1.1. Try it in the AppletViewer for a JVM >= 1.2 and it should work.
Norm
Dear Norm
Thank you for reply. As you ask I use this things.
JDK 1.2.2
Internet Explorer Version: 6.0.2462.0000
OS Name: Microsoft Windows Whistler Beta Professional
When I try to Compile my Code it give me this error:
MySound.java:20: Can’t make static reference to method java.applet.AudioClip getAudioClip(java.net.URL, java.lang.String) in class java.applet.Applet.
My Code is:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
/*
<applet code="MySound" width=200 height=200>
</applet>
*/
public class MySound extends Applet implements ActionListener {
Button bPlay;
AudioClip audioClip;
public void init() {
bPlay = new Button("Play Sound");
audioClip = Applet.getAudioClip(getCodeBase(),"gradually.wav");
add(bPlay);
bPlay.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == bPlay) {
audioClip.play();
}
}
}
To correct the compiler error you get, you need to read the documentation for the getAudioClip() method. Is it a static method of the Applet class? Or is it non-static? To call a class's static method you code: classname.methodname();
For example: int x = Math.min(1, 2); calls the Math class's min() method which is static.
To call a class's non-static method you code: methodname();
For example in an Applet you code: Image img = getImage(url); to get an image object.
Norm
At last I done it. Hear how I done it
/**
* Java code for playing sound on an Applet.
* @author Ajay Parmar
* e-mail: [email protected]
*/
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
/*
<applet code="MySound" width=200 height=200>
</applet>
*/
public class MySound extends Applet implements ActionListener {
Button bPlay;
AudioClip audioClip;
URL clipPath;
public void init() {
bPlay = new Button("Play Sound");
try {
clipPath = new URL(this.getCodeBase()+"MySound.wav");
}
catch(Exception e) { }
audioClip = this.newAudioClip(clipPath);
add(bPlay);
bPlay.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == bPlay) {
audioClip.play();
}
}
}
to solve the problem that you mentioned
just replace the following line in your code:
<font color=red>audioClip = Applet.getAudioClip(getCodeBase(),"gradually.wav");</font>
with this 1
<font color=red>audioClip = getAudioClip(getCodeBase(),"gradually.wav");</font>
Dear
Thank you very much for your suggestion. I had done that before. Actually I replace Applet word with this so it work well. Any way thank you very much for your replay. My code is as under:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import java.net.URL;
/*
<applet code="MySound" width=200 height=200>
</applet>
*/
public class MySound extends Applet implements ActionListener {
Button bPlay;
AudioClip audioClip;
URL clipPath;
public void init() {
bPlay = new Button("Play Sound");
try {
clipPath = new URL(this.getCodeBase()+"gradually.wav");
}
catch(Exception e) { }
audioClip = this.newAudioClip(clipPath);
add(bPlay);
bPlay.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if(ae.getSource() == bPlay) {
audioClip.play();
}
}
}
Ajay Parmar