Simple method to play a sound file?
I'm a VB programmer, so this is completely new to me this java.
I'm creating an educational game for grade school kids and I've selected java for it's portability.
This application will feature a "sonar" of sorts. There are two 1K sound files (currently in wav format, but I don't care what format to use as long as it's easy). From far a way, sound1 will play followed by a short pause and then sound2 will play. As the game progresses, the pause between the two sounds gradually gets shorter and shorter and shorter...much like a regular sonar.
I've looked through countless examples here and with google and it just amazes me, why isn't there a SIMPLE and EASY method to play a simple, small sound file?
How can I accomplish what I want with just a few lines of code?
Remember, I'm a greenhorn to java... please be gentle!
Thank you
Ted
[897 byte] By [
trwagner1a] at [2007-11-26 19:05:47]

# 1
Her is a list of examples related to sound in Java. One of them should be (or close to) what you want.
http://www.google.com/custom?domains=exampledepot.com&q=sound&sitesearch=exampledepot.com&client=pub-6001183370374757&forid=1&ie=ISO-8859-1&oe=ISO-8859-1&cof=GALT%3A%23008000%3BGL%3A1%3BDIV%3A%23336699%3BVLC%3A663399%3BAH%3Acenter%3BBGC%3AFFFFFF%3BLBGC%3A336699%3BALC%3A0000FF%3BLC%3A0000FF%3BT%3A000000%3BGFNT%3A0000FF%3BGIMP%3A0000FF%3BFORID%3A1%3B&hl=en
# 2
Ok, given the xamples you gave, here's what I've come up with:
package sound20070220;
import java.applet.*;
import java.net.*;
public class Main {
/** Creates a new instance of Main */
public Main() {
}
public static void main(String[] args) {
try {
// Load audio clip
URL url1 = new URL("file://C:/projects/SoundTest/bip.wav");
URL url2 = new URL("file://C:/projects/SoundTest/bop.wav");
AudioClip bip = Applet.newAudioClip(url1);
AudioClip bop = Applet.newAudioClip(url2);
bip.play();
bip.stop();
bop.play();
bip.stop();
} catch (MalformedURLException ex) {
ex.printStackTrace();
}
}
}
However, when I debug and run the app, no sound is played.
What mystifies me is that it takes so much code to accomplish something compared to other tools/IDE's such as MS VB expres or VB Studio..
What am I doing wrong?
# 4
Well, after quite a bit more research, I came up with the following class file and am loading it from an index.html file.
import java.applet.*;
import java.awt.event.*;
import java.awt.*;
public class SoundExample extends Applet {
AudioClip soundFile1;
AudioClip soundFile2;
public void init()
{
soundFile1 = getAudioClip(getDocumentBase(),"ping1.wav");
soundFile2 = getAudioClip(getDocumentBase(),"ping2.wav");
soundit();
}
private void pause(int time) {
try { Thread.sleep(time);
}
catch (InterruptedException e) {
}
}
private void soundit(){
soundFile1.play();
pause(1000);
soundFile2.play();
}
}
What I'm confused about at this point is that, A) I get soundFile1.play to work, but the pause and soundFile2.play doesn't follow.
I want that distinctive sound of the low volume "ping" and the second louder file "PING" to sound.
What am I doing wrong here? It's something to do with my pause method...if I change soundFile1 to point to the other sound file, it plays the other file...it just never does the sleep and plays the second file.
Sound sound like "ping (1 second pause) PONG"
Thanks
Ted