1

I have to get a .wav clip working for a college assignment on Java using the AudioClip and Applet. When I use the code seen below, I get no errors, even with the use of a try-catch. Not really sure where I have gone wrong, but any help would be much appreciated!

class PlayMySoundApplication {
    static void playElevatorMusic() {
        try {
            AudioClip sound;
            File file = new File ("elevatorMusic.wav");
            URL wavFile = file.toURI ().toURL ();

            sound = Applet.newAudioClip (wavFile);
            sound.play ();
        } catch (Exception e) {
            e.printStackTrace ();
        }
    }

    public static void main(String[] args) {
        playElevatorMusic ();
    }
}
jose praveen
  • 1,298
  • 2
  • 10
  • 17
  • It is likely the audio clip is being run on a daemon thread which is ended once the `main` method is complete. To confirm this, change `playElevatorMusic ();` to `playElevatorMusic (); JOptionPane.showMessageDialog(..);`. – Andrew Thompson Mar 23 '20 at 02:13
  • 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/). – Andrew Thompson Mar 23 '20 at 02:13

0 Answers0