Play Audio in Java Applet

Java has the feature of the playing the sound file. This program will show you how to play a audio clip in your java applet viewer or on the browser.

Play Audio in Java Applet

Java has the feature of the playing the sound file. This program will show you how to play a audio clip in your java applet viewer or on the browser.

Play Audio in Java Applet

Play Audio in Java Applet

     

Introduction

Java has the feature of the playing the sound file. This program will show you how to play a audio clip in your java applet viewer or on the browser.

For this example we will be creating an applet called PlaySoundApplet.java to play sound. There are two buttons to play the sound in Loop and to Stop the sound. 

The play() method of AudioClip object is used to play the sound while stop() method is used for stop the running audio clip suddenly.

Here is the code of the program : 

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()"TestSnd.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();
  }
  }
}

AudioClip class:

In this example we have a class AudioClip, which is an abstract class. So, it can't be instantiated directly. But there a method called getAudioClip() of Applet class which can be used to create the object of AudioClip. There are two  versions of getAudioClip() function:

  1. public AudioClip getAudioClip(URL url)
      
  2. public AudioClip getAudioClip(URL url, String name)

In this example we are using the second method:

audioClip = getAudioClip(getCodeBase(), "TestSnd.wav");

AudioClip class provides the following methods: 

public abstract void play()  - to play the sound only once
public abstract void loop()  - to play the sound in loop
public abstract void stop()  - to stop the playing sound 

Here is the HTML code :

<HTML>
<BODY>
<APPLET CODE="PlaySoundApplet" WIDTH="200" HEIGHT="300"></APPLET>
</BODY>
</HTML>

Try online this example

Download Example Code

Download Sound File Used in the Program

Tutorials

  1. What is an Applet
  2. The Life cycle of An Applet
  3. Java Applet - Creating First Applet Example
  4. Java - Drawing Shapes Example in java
  5. Java - Drawing Shapes Example using color in java
  6. Java - Event Listeners Example in Java Applet
  7. Applet - Passing Parameter in Java Applet
  8. Opening a URL from an Applet
  9. Java - Opening a url in new window from an applet
  10. Applet is not Working
  11. Display image in the applet
  12. Applet Write Files Example
  13. Play Audio in Java Applet
  14. Security Issues with the Applet
  15. Swing Applet Example in java
  16. The Sample Banner Example in Java
  17. Clock Applet in Java
  18. HTML Document Creation
  19. Tag Parameters: The Tag
  20. The APPLET Tag in Detail
  21. Java and HTML: The Basics
  22. What Exactly is HTML?
  23. Welcome to the Internet
  24. java.applet package examples
  25. java.applet package examples
  26. What is an Applet - Java Applet Tutorial
  27. Java - Read file Applet
  28. Applet versus Application
  29. Tag Parameters: The Tag
  30. Java - Opening a url in new window from an applet
  31. Java - Opening a URL from an Applet
  32. Applet Tag Parameters,Applet Tag in HTML
  33. Applets in Java
  34. What is Applet in Java?
  35. What is Applet in Java with Example?