Dwane Brown Jr
Dwane Brown Jr

Reputation: 55

Why is the applet not playing the audio clip?

I have an applet that is supposed to make a sound on click but it does not work. It would be really helpful if someone could point out what is wrong with the code

 AudioClip soundFile1; 
 AudioClip soundFile2;

 public void init()  
 {
      soundFile1 = getAudioClip(getDocumentBase(),"volcanoe.au"); 
      soundFile2 = getAudioClip(getDocumentBase(),"volcanoe.au");
      addMouseListener(this); 
      setBackground(Color.yellow); 
      soundFile1.play(); 
 }

 public void paint(Graphics g)  
 { 
      g.drawString("Click to hear a sound",20,20); 
 }
 public void mouseClicked(MouseEvent evt)  
 { 
      soundFile2.play(); 
 }
 public void mousePressed(MouseEvent evt) {} 
 public void mouseReleased(MouseEvent evt) {} 
 public void mouseEntered(MouseEvent evt) {} 
 public void mouseExited(MouseEvent evt) {} 

Upvotes: 0

Views: 863

Answers (2)

user5794376
user5794376

Reputation:

I ran your code on my computer and it worked perfectly. The audio clip stored in soundFile1 starts playing when the applet is run and when the mouse is clicked, the audio that was already playing stops and the new audio clip in soundFile2 starts playing. I tested the program with a .wav and a .au file. The problem in your case might be one of the following:

  1. The audio file is missing from the directory where the .class file of your applet is. The audio file should be present in the directory of the current project you're working on.

  2. There's something wrong with the .au file that exists in the current directory. Test the applet with other file formats (eg. .wav) or another .au file.

  3. Something is wrong with your computer's audio codecs. Look up on the internet to fix your audio codecs.

  4. Try re-installing your JDK. As @Tbtimber said, try using getCodeBase() instead of getDocumentBase(). If it works, then update/re-install your JDK.

Upvotes: 1

Tbtimber
Tbtimber

Reputation: 41

Try using getCodeBase() instead of getDocumentBase() when you create the AudioClip.

Upvotes: 0

Related Questions