Playing music using applet in java

Introduction 

 
In this blog we will know how to play, repeat and stop playing music using applet.
  1. //Applet to play sound    
  2. import java.applet.*;    
  3. import java.awt.*;    
  4. import java.net.*;    
  5. import java.awt.event.*;    
  6.      
  7. public class Musicapplet extends Applet implements ActionListener    
  8. {    
  9.     Button b1,b2,b3;    
  10.     AudioClip au;    
  11.   public void init()    
  12.      {    
  13.         Font f=new Font("Arial",Font.BOLD,25);    
  14.         setFont(f);    
  15.    try{ URL u1=new URL("file:///E:/java/airtel.au");    
  16.     au=getAudioClip(u1);    
  17.    b1=new Button("Play");    
  18.    b2=new Button("Repeat");    
  19.    b3=new Button("Stop");    
  20.      
  21. add(b1);add(b2);add(b3);    
  22.    b1.addActionListener(this);    
  23.    b2.addActionListener(this);    
  24.    b3.addActionListener(this);    
  25.     Label   lab1=new Label("Playing  music");    
  26.        
  27.     setBackground(Color.yellow);    
  28.     add(lab1);    
  29. }    
  30. catch(Exception  e){    
  31.    System.out.println(e);    
  32.     }    
  33.      
  34.    }    
  35.   public void actionPerformed(ActionEvent ae)    
  36.    {    
  37.    Button b=(Button)ae.getSource();    
  38.    if(b==b1)    
  39.       au.play();    
  40.     else if(b==b2)    
  41.       au.loop();    
  42.     else    
  43.       au.stop();    
  44.     }    
  45. }    
  46.      
  47. /*   
  48.  <applet code="Musicapplet" height="200"  width="200">   
  49. </applet>   
  50.   */       

Compile

Make a directory java in any drive (E:\java). Store music files and java file into that directory.
 
Open command prompt and go to that directory for compiling the java file as
 
E:\Documents and Settings\Administrator>cd\
E:\>cd E:\java
E:\java>javac Musicapplet.java
E:\java>appletviewer Musicapplet.java
 
Enjoy the music using applet!!!!