Saturday, 28 September 2019

How to develop a sound recorder program in Java Swing

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:
    • How to record sound:
      • Capture and record sound into WAV file with Java Sound API.
      • A utility for recording sound in Java.
    • How to play back sound: How to play back audio in Java with examples.
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).

No comments:

Post a Comment