0

I put together this applet to play sound but for some reason the sound is not playing. It's probably something simple, but what am I doing wrong here? I added the sound to the right location, and I am not getting any errors, but I am not getting anything working.

import java.applet.Applet;
import java.applet.AudioClip;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.URL;

public class MediaPlayer extends Applet implements ActionListener{
    Button playBtn;
    Button pauseBtn;
    Button loopBtn;
    URL soundPath;
    AudioClip sound;

    public void init() {
        playBtn = new Button("Play");
        add(playBtn);
        playBtn.addActionListener(this);

        pauseBtn = new Button("Pause");
        add(pauseBtn);
        pauseBtn.addActionListener(this);

        loopBtn = new Button("Loop");
        add(loopBtn);
        loopBtn.addActionListener(this);
        try{
            soundPath = new URL(getCodeBase(),"office.wav");
        }catch(Exception e){
            System.out.print(e.toString());
        }
        sound = Applet.newAudioClip(soundPath);

     }

    public void actionPerformed(ActionEvent e) {
        if(e.getSource() == playBtn) sound.play();
        if(e.getSource() == pauseBtn) sound.stop();
        if(e.getSource() == loopBtn) sound.loop();
    }

}

DezOnlyOne
  • 95
  • 2
  • 10
  • 1
    The sound should play in the other thread. – Roman C Oct 23 '15 at 13:56
  • 1) 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. 2) 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/). 3) See [Detection/fix for the hanging close bracket of a code block](http://meta.stackexchange.com/q/251795/155831) for a problem I could no longer be bothered fixing. – Andrew Thompson Oct 23 '15 at 17:36
  • 4) Be sure the [Java Console](http://www.java.com/en/download/help/javaconsole.xml) is configured to show. If there is no output at the default level, raise the level and try it again. 5) *"Having trouble with a simple Java Applet with sound"* As an aside, applets are often said to be 'simple' but they have become very 'difficult'. Take it from the author of that blog article above. ;) – Andrew Thompson Oct 23 '15 at 17:44

0 Answers0