This tutorial presents a sample Sound Recorder program written in Java Swing and provides step-by-step guide on how to develop such one. The program looks like this:

 Swing Sound Recorder program

The program is working as follows:

  • Click Record button to start recording, the record time is counting:Swing Sound Recorder - recording

  • Click Stop to end recording, the program will ask for saving the sound:Swing Sound Recorder - saving

  • After saving the recorded sound into a WAV file, the Play button is enable to allow playing back:Swing Sound Recorder - after saving

  • Click Play to start playing the recent recorded sound, the time is counting again like this:Swing Sound Recorder - playing back

  • Click Stop to stop playing back or wait until the playback completes:
Swing Sound Recorder - stop playing back

Now you can repeat the same steps to record another one, cool right? Let’s see how to build this nice program in Java.



For technical details, refer to the following tutorials:

The following class diagram explains how the program is designed:

 Swing Sound Recorder class diagram

As we can see, the program consists of four main classes:

    • SoundRecordingUtil.java: implements functionalities to start recording, stop recording and save the recorded sound into a WAV file. Code that calls the start()method should be executed in a separate thread, because it blocks the current thread until a call to stop() method is made. Doing so to make the GUI does not freeze when the recording is taking place.
    • AudioPlayer.java: implements functionalities to start and stop playing back the recorded sound saved in the WAV file. Code that call the play() method should be executed in a separate thread, similar to the situation of the SoundRecordingUtil class.
    • RecordTimer.java: implements a function to count up the record/playback time in the format of HH:mm:ss e.g. 00:02:45 (0 hours and 2 minutes and 45 seconds). There should be a separate thread to run this task.
    • SwingSoundRecorder.java: this is the main program that constructs all the graphical user interface stuffs and wires all the above classes together to form a nice and functional program.
To explore details of each class, download Eclipse-based project the program (zip archive) and look at the src directory. You can also try to run the program by downloading and executing the program’s jar file in the attachments section (require Java 7 or later).

 

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 (SwingSoundRecorder.jar)SwingSoundRecorder.jar[Executable JAR file]12 kB
Download this file (SwingSoundRecorder.zip)SwingSoundRecorder.zip[Eclipse project]21 kB

Add comment

   


Comments 

#6Tanek2021-07-11 12:46
When it is time to stop the audio recording and save the file, the method stop() of class SoundRecordingUtil causes the thread to stop working.
Quote
#5Tony Okeke2021-04-23 15:54
I want to get follow-up comments
Quote
#4Dani2020-11-20 13:58
Project is error... When i stop recording... The project stop working...
Quote
#3Ihaz2020-05-15 05:12
Code source please
My question how to capture number of ligne
Quote
#2DaFaka2018-12-12 08:38
Hi, do you found a way to pause the record and continue recording the same file?
Quote