Record Audio with a MediaRecorder

To see the audio file you recorded (audiorecordtest.3gp), launch the File Manager app and go to the Music folder. This app is a remodularized version of this example and the code snippet in the “common case” here.

Source code in Record.zip

  1. MainActivity.java
  2. activity_main.xml
  3. strings.xml
  4. AndroidManifest.xml uses-permission RECORD_AUDIO and WRITE_EXTERNAL_STORAGE.
  5. build.gradle (Module: app)

Examine the audio file

adb devices

adb -s 192.168.57.101:5555 shell find / -type f -name audiorecordtest.3gp
/mnt/shell/emulated/0/Music/audiorecordtest.3gp
/data/media/0/Music/audiorecordtest.3gp

adb -s 192.168.57.101:5555 pull /mnt/shell/emulated/0/Music/audiorecordtest.3gp
3027 KB/s (6656 bytes in 0.002s)

ls -l audiorecordtest.3gp
-rw-r--r--  1 myname  mygroup  6656 Aug 28 23:31 audiorecordtest.3gp

file audiorecordtest.3gp
audiorecordtest.3gp: ISO Media, MPEG v4 system, 3GPP

Things to try

  1. MediaPlayer.prepare takes too long to call in the UI thread. Instead, call prepareAsync and use a MediaPlayer.OnPreparedListener as in MediaPlayerService. Why doesn’t class MediaRecorder have a prepareAsync method or a MediaRecorder.OnPreparedListener class?

  2. During playback, detect the end of the audio file as in MediaPlayer, and change the button back to “start playing”.

  3. Display an odometer during recording and playback, as in MediaPlayer.

  4. Display a red light while recording is in progress. Or an animation of two reels of tape turning.

  5. Disable the play button until an audio file has been recorded. Disable the play button whenever recording is in progress. Disable the record button whenever playback is in progress.

  6. Ask the user what the name of the audio file should be. Use an EditText, maybe mounted in a Dialog.

  7. You know how a sink can have one lever that controls both the temperature and the volume of the water? Is there a way we could have one lever that controls the starting and stopping of the recording and playback?