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:

    • AudioPlayer.java: this is a utility class that provides primary functionalities for playing back an audio file like play, stop, pause, and resume. It is based on the Java Sound API. This class is an enhanced version of the technique discussed in the tutorial: How to play back audio in Java with examples. The enhancements are for working in a Swing-based application. A separate thread should be spawned to use this utility class.
    • PlayingTimer.java: this thread-based class is responsible to generate current playing time during the playback. It also updates the slider accordingly.
    • SwingAudioPlayer.java: this is the main program which is based on a JFrame. It constructs the user interface and wires to AudioPlayerand PlayingTimer together to form a nice-looking and functional audio player program.

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 began programming with Java back in the days of Java 1.4 and has been passionate about it ever since. You can connect with him on Facebook and watch his Java videos on YouTube.



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

Add comment

   


Comments 

#34Vishal Srivastava2021-01-20 02:43
There's no special license. You can use the code freely with attribution for my name
Quote
#33Nam2020-04-06 22:49
Hi Nat R. There's no special license. You can use the code freely with attribution for my name and website.
Quote
#32Nat R.2020-04-05 12:29
I'm working on a group project with MIDI files, but parts of this code would be very useful for that. Does this have a copyright license on it? Is there a creative commons thingamajiggy? I'd appreciate it if you'd let me know. Thanks!
Quote
#31Nam2020-03-02 21:37
Hi John,
Yes, it's very possible to do that. You can embed a WAV file inside the JAR and read it as a resource stream.
Quote
#30Randy2020-02-29 08:08
Hello sir, there is a bug when opening file while playing. Please check it.
Quote