Home Java Example Java Applet Play Audio in Java Applet
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Play Audio in Java Applet

Advertisement
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

     

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

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: April 14, 2007

Ask Questions?    Discuss: Play Audio in Java Applet   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
Ishita divya
May 27, 2011
comment on codes for sound

I tried to run the above code(for playing audio clip) but i could not. I saved html and audio files but it is not working. Please give me solution for this.
daljeet
July 9, 2011
audiofile

how can i play a full audio song its not playing where I am wrong
jayesh
July 1, 2011
audio in applet

how to use url in method ?
sikandar kumar
July 29, 2011
programm

audioClip = getAudioClip(getCodeBase(), "TestSnd.wav"); where written inaddress of audiofile
MOHSIN KHAN
June 2, 2013
play sound on click on image

hey i tried the above code and it work fine and sound play successful thank you very much.but i want image .when i clicked on image the sound should be played . eg. if i click on cat image it should play cat sound and if i clicked on dog image it should play dog bow bow. i'll be glade if you help me....
kanhaiya
December 11, 2011
How to link java applicaton parogram

Plz help me that how to link one to anther java applicaton parogram . i have created two java but i am not link these program so help me plz plz
UFAQ
January 14, 2012
error in playing audio

I want to play an audio your giving whole code is working but in method audioClip = getAudioClip(getCodeBase(), "TestSnd.wav"); in getCodeBase() i got error. it returns URL class object how to handle this and where i should pass my audio file path
Ajit Kumar Pradhan
January 29, 2012
Regarding Applet

Whenever a program is developed to play an audio in applet,first where to place the audio file in my computer?
K.Ramesh kumar
February 2, 2012
sample program

Please, give me sample program for insert Audio(sound) on Frame in java
atul
April 5, 2012
audio play

its not working sound notplayed..bro
pooja sharma
July 29, 2012
play sound in applet

it is not working for mp3 sound...
vijay
September 4, 2012
feedback

thanks for this code
James Morozek
May 6, 2013
Reply

For the actionPerformed(ActionEvent ae) { ... } I needed to use the following code for it to compare String. public void actionPerformed(ActionEvent ae) { Button source = (Button)ae.getSource(); if (source.getLabel().equals("p")) { audioClip.play(); } else if (source.getLabel().equals("s")) { audioClip.stop(); } }
Rod
December 1, 2013
ClassNotFoundException

When I try to use this I get ClassNotFoundException.
pooja sharma
July 29, 2012
play sound in applet

your code work for only .wav sound.... not for mp3 sound for this what is solution....
DMCA.com