Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
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

                         

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

                         

» View all related tutorials
Related Tags: html c com orm air tags sed char tag rounding ai element tar character elements characters like start call cte

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

1 comments so far (
post your own) View All Comments Latest 10 Comments:

its to good

Posted by chandravir on Tuesday, 12.16.08 @ 10:50am | #82834

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.