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. public class Musicapplet extends Applet implements ActionListener
  7. {
  8. Button b1,b2,b3;
  9. AudioClip au;
  10. public void init()
  11. {
  12. Font f=new Font("Arial",Font.BOLD,25);
  13. setFont(f);
  14. try{ URL u1=new URL("file:///E:/java/airtel.au");
  15. au=getAudioClip(u1);
  16. b1=new Button("Play");
  17. b2=new Button("Repeat");
  18. b3=new Button("Stop");
  19. add(b1);add(b2);add(b3);
  20. b1.addActionListener(this);
  21. b2.addActionListener(this);
  22. b3.addActionListener(this);
  23. Label lab1=new Label("Playing music");
  24. setBackground(Color.yellow);
  25. add(lab1);
  26. }
  27. catch(Exception e){
  28. System.out.println(e);
  29. }
  30. }
  31. public void actionPerformed(ActionEvent ae)
  32. {
  33. Button b=(Button)ae.getSource();
  34. if(b==b1)
  35. au.play();
  36. else if(b==b2)
  37. au.loop();
  38. else
  39. au.stop();
  40. }
  41. }
  42. /*
  43. <applet code="Musicapplet" height="200" width="200">
  44. </applet>
  45. */

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!!!!