Java Program to Play Audio using Applet

This is a Java Program to Play Sound Using Applet

Problem Description

We have to write a program in Java such that it creates a music player to pause, play, stop and replay a sound.

Expected Input and Output

The audio file is saved as “audio.wav”

Consider buttons are created with the following image icons :

For Pause Button : The icon is saved as icon_pause.png
java-sound-icon_pause

For Play Button : The icon is saved as icon_play.png
java-sound-icon_play

For Stop Button : The icon is saved as icon_stop.png
java-sound-icon_stop

advertisement
advertisement

For Replay Button : The icon is saved as icon_replay.png
java-sound-icon_replay

For playing sound using applet, we can have the following 5 different sets of input and output.

1. To Play the Sound : Before the sound is Paused

When the Play button is clicked,
it is expected that the sound is played.

2. To Pause the Sound :

When the Pause button is clicked and sound is being played,
it is expected that the sound is paused.

3. To Play the Sound : After the sound is Paused

When the Play button is clicked,
it is expected that the sound is played from the paused position.

4. To Stop the Sound :

advertisement
When the Stop button is clicked,
it is expected that the sound is stopped.

5. To Replay the Sound :

When the Replay button is clicked,
it is expected that the sound is started from the beginning.
Problem Solution

1. Create buttons with icons for the options – pause, play, stop and replay.
2. Load the sound file using AudioInputStream.
3. Create a object of class Clip to load the sound in a data line.
4. When the button pause is clicked, stop the clip and store the last position of the clip.
5. When the button play is clicked, start the clip from the last position of being stopped. In case the sound is never stopped, play from the beginning.
6. When the button stop is clicked, stop the clip and reset the last position to the beginning.
7. When the button replay is clicked, start the clip from the beginning.

Program/Source Code

Here is source code of the Java Program to play a sound using an applet. The program is successfully compiled and tested using javac compiler on Fedora 30. The program output is also shown below.

advertisement
  1. /* Java Program to Play Sound using Applet */
  2. import java.awt.*;
  3. import java.applet.*;
  4. import javax.swing.*;
  5. import java.awt.event.*;
  6. import javax.sound.sampled.*;
  7. import java.io.File;
  8. public class Music extends Applet implements ActionListener
  9. {
  10.     ImageIcon pause,play,stop,replay;
  11.     long current;
  12.     Clip clip;
  13.     //Initialize the applet
  14.     public void init()
  15.     {
  16. 	setBackground(Color.white);
  17. 	//Create image icons for the buttons
  18. 	pause = new ImageIcon("icon_pause.png");
  19. 	play = new ImageIcon("icon_play.png");
  20. 	stop = new ImageIcon("icon_stop.png");
  21. 	replay = new ImageIcon("icon_replay.png");
  22.     }
  23.     //Add buttons to applet and load the sound
  24.     public void start()
  25.     {
  26. 	JButton b_pause = new JButton(pause);
  27. 	this.add(b_pause);
  28. 	b_pause.addActionListener(this);
  29.  
  30. 	JButton b_play = new JButton(play);
  31. 	this.add(b_play);
  32. 	b_play.addActionListener(this);
  33.  
  34. 	JButton b_stop = new JButton(stop);
  35. 	this.add(b_stop);
  36. 	b_stop.addActionListener(this);
  37.  
  38. 	JButton b_replay = new JButton(replay);
  39. 	this.add(b_replay);
  40. 	b_replay.addActionListener(this);
  41.  
  42. 	try
  43. 	{
  44. 	    AudioInputStream audio;
  45. 	    File file = new File ("sound.wav");
  46. 	    audio = AudioSystem.getAudioInputStream(file);
  47. 	    clip = AudioSystem.getClip();
  48. 	    clip.open(audio);
  49. 	    current=0L;
  50. 	}
  51. 	catch(Exception E)
  52. 	{
  53. 	    System.out.println(E.getMessage());
  54. 	}
  55.     }	
  56.     //Function to perform the selected option
  57.     public void actionPerformed(ActionEvent e)
  58.     {
  59. 	String icon = ((JButton)e.getSource()).getIcon().toString();
  60.  
  61. 	if(icon.equals(pause.toString()))
  62. 	{
  63. 	    current = clip.getMicrosecondPosition();
  64. 	    clip.stop();
  65. 	}
  66. 	else if(icon.equals(play.toString()))
  67. 	{
  68. 	    clip.setMicrosecondPosition(current);
  69. 	    clip.start();
  70. 	}
  71. 	else if(icon.equals(stop.toString()))
  72. 	{
  73. 	    current = 0L;
  74. 	    clip.setMicrosecondPosition(0);
  75. 	    clip.stop();
  76. 	}
  77. 	else
  78. 	{
  79. 	    current = 0L;
  80. 	    clip.setMicrosecondPosition(0);	
  81. 	    clip.start();
  82. 	}
  83.  
  84.     }
  85. }
  86. /*
  87. <applet code = Music.java width=750 height=400>
  88. </applet>
  89. */

To compile and execute the program use the following commands :

>>> javac Music.java
>>> appletviewer Music.java
Program Explanation

1. The class AudioInputStream is used to load an audio file.
2. To load an audio file to stream, use AudioSystem.getAudioInputStream(File).
3. To convert stream to data line, use AudioSystem.getClip().
4. To get the current position of the clip, use getMicrosecondPosition method.
5. To set the position of the clip, use setMicrosecondPosition(long) method.
6. To start the audio, use clip.start()
7. To stop the audio, use clip.stop()

Runtime Test Cases

Here’s the run time test cases for playing a sound using an applet.

Test case 1 – To View the Applet
java-applet-play-music

Sanfoundry Global Education & Learning Series – Java Programs.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.