In this article, we would like to present a Swing-based application that is able to play back audio files (in WAV (*.wav) format) and show you how to build one. This audio player program will look like this:

Java Audio Player - start up

The program is working just like a typical audio player. First, let’s see how this program is supposed to work. Click the Open button to pick an audio file:

 Java Audio Player - Open audio file

Note that this audio player program is built based on pure Java Sound API so it can play only *.wav files (other supported formats are *.aifc, *.aiff, *.au and *.snd but these are less popular). Select a file in the dialog then click Open, the program will play back the audio file:

 Java Audio Player - playing

You can notice that duration of the audio file is shown on the right most (in form of hour:minute:second). The current playing time is shown on the left most. During the playback, the slider is moving gradually from the left to the right to indicate the progress. Click the Pause button to temporarily pause the playback:

 Java Audio Player - paused

The slider stops moving and the Pause button’s label becomes Resume which will continue the playback when clicked.



Click the Stop button to terminate playing the audio:

 Java Audio Player - stopped

The slider moves to the beginning position and the Stop button becomes Play button which will start over the audio playback when clicked.

During the playback we can click the Open button to choose another audio file. In this case, the current playback will be stopped and start playing the newly chosen audio file.

Now let’s see how this pretty nice program is built. The following class diagram describes how the program is designed:

 Audio Player class diagram

The program consists of three classes:

It’s not convenient for listing the entire program’s source code here. So you can study this program by downloading the below attachment which includes an executable jar file and full project’s source code. Enjoy!

 

Related Java Sound Tutorials:

 

Other Java Tutorials:


About the Author:

is certified Java programmer (SCJP and SCWCD). He started programming with Java in the time of Java 1.4 and has been falling in love with Java since then. Make friend with him on Facebook and watch his Java videos you YouTube.



Attachments:
Download this file (SwingAudioPlayer.zip)SwingAudioPlayer.zip[Full source code and executable jar file]30 kB