Skip to content
Search

Android.media.MediaRecorder

lastmod 2018-08-17created 2016-05-206 edits

Recoding step

Basic camcorder recording steps

  1. Unlock the camera with Camera.unlock().
  2. Configure MediaRecorder as shown in the code example above.
  3. Start recording using MediaRecorder.start().
  4. Record the video.
  5. Stop recording using MediaRecorder.stop().
  6. Release the media recorder with MediaRecorder.release().
  7. Lock the camera using Camera.lock().
Mediarecorder_state_diagram.gif

List of methods

setAudioSource()
Sets where the sound comes from. Decides whether to record the microphone, the voices during a call, only the caller's voice during a call, only the receiver's voice during a call, and so on.
setOutputFormat()
Sets the data format of the recording file.
setAudioEncoder()
Sets the encoder (codec).
setOutputFile()
Sets the path and name of the recording file.

Example

Sample recording example:

private static void record() {
    if (recorder != null) {
        recorder.stop();
        recorder.release();
        recorder = null;
    }
    recorder = new MediaRecorder();
    recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    recorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
    recorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    recorder.setOutputFile(filePath);

    try {
        recorder.prepare();
        recorder.start();
        Log.i(TAG, "recording start");
    } catch (IllegalStateException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Android MediaRecorder system architecture

Android_media_recorder_system_architecture.png

Troubleshooting

Start failed -16

When recording captured screens with MediaRecorder and Camera, the error start failed: -16 can occur on the Nexus S.

When using the front camera, setting the quality of android.media.CamcorderProfile to High and calling start() on the Recorder causes this error.

Stop failed -1007

The following error can occur.

E/MediaRecorder(15709): stop failed: -1007
java.lang.RuntimeException: stop failed.
at android.media.MediaRecorder.stop(Native Method)
...
  • that a RuntimeException is intentionally thrown to the application, if no valid audio/video data has been received when stop() is called.
  • This happens if stop() is called immediately after start().
  • Solved it at last. The issue was setting the preview size before setting the actual preview for the camera. The preview size MUST be equal to the selected video size.

See also

Favorite site

References


  1. Android_media_recorder_system_architecture_stream.pdf ↩

  2. Android_mediarecorder_error.pdf ↩