0

I am attempting to add sound to a game, I've tried

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class PlaySoundApplet extends Applet 
implements ActionListener{
   Button play,stop;
   AudioClip audioClip;
   public void init(){
      play = new Button("  Play in Loop  ");
      add(play);
      play.addActionListener(this);
      stop = new Button("  Stop  ");
      add(stop);
      stop.addActionListener(this);
      audioClip = getAudioClip(getCodeBase(), "Sound.wav");
   }
   public void actionPerformed(ActionEvent ae){
      Button source = (Button)ae.getSource();
      if (source.getLabel() == "  Play in Loop  "){
     audioClip.play();
      }  
      else if(source.getLabel() == "  Stop  "){
         audioClip.stop();
      }
   }
}

but that interferes with my Keylistener for some reason. I've also tried How can I play sound in Java?, every solution there possible but those either deal with JFrame/Canvas or does not work. I've also encountered this error:

Mini.java:8: warning: AppletAudioClip is internal proprietary API and may be removed in a future release import sun.applet.AppletAudioClip;

I'm not quite sure what that error is, but in any case, I'm looking for a code that I can add to my existing program - preferably with a mute button available, but it's not necessary.

Community
  • 1
  • 1
  • 1) Why code an applet? If it is due to the teacher specifying it, please refer them to [Why CS teachers should **stop** teaching Java applets](http://programmers.blogoverflow.com/2013/05/why-cs-teachers-should-stop-teaching-java-applets/). 2) Why use AWT? See [this answer](http://stackoverflow.com/questions/6255106/java-gui-listeners-without-awt/6255978#6255978) for many good reasons to abandon AWT using components in favor of Swing. 3) Use [Java Sound](https://docs.oracle.com/javase/tutorial/sound/) for this - it works in applets or applications. .. – Andrew Thompson May 23 '16 at 04:03
  • .. 4) *"but that interferes with my Keylistener for some reason"* Programming is not magic. There is a reason it interferes with the key listener, and you should figure out what that problem is! 5) *"I've also encountered this error:"* Not in that code above, you haven't. On compilation it produces 4 warnings here (2 overrides notation, 2 using `==` for string comparison), none of which refer to classes it does not import. – Andrew Thompson May 23 '16 at 04:04
  • what exactly is the problem you're having? Are you able to play the audio clip? Your code didn't seem to be problematic on my computer. –  May 23 '16 at 07:00

0 Answers0