-
August 13th, 2001, 02:48 AM
#1
Playing Sound from Applet
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.
-
August 13th, 2001, 10:13 AM
#2
Re: Playing Sound from 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
Norm
-
August 13th, 2001, 11:24 AM
#3
Re: Playing Sound from Applet
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();
}
}
-
August 13th, 2001, 09:32 PM
#4
Re: Playing Sound from Applet
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
Norm
-
August 14th, 2001, 02:58 AM
#5
Re: Playing Sound from Applet
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();
}
}
}
-
August 14th, 2001, 10:07 AM
#6
static vs non-static method calls
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
Norm
-
August 15th, 2001, 10:10 AM
#7
Re: static vs non-static method calls
At last I done it. Hear how I done it
/**
* Java code for playing sound on an Applet.
* @author Ajay Parmar
* e-mail: pndatasystem@yahoo.com
*/
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();
}
}
}
-
September 5th, 2001, 05:27 AM
#8
Re: Playing Sound from Applet
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>
-
September 7th, 2001, 02:17 AM
#9
Re: Playing Sound from Applet
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|