Android.media.MediaRecorder
lastmod 2018-08-17created 2016-05-206 edits
Recoding step
Basic camcorder recording steps
- Unlock the camera with Camera.unlock().
- Configure MediaRecorder as shown in the code example above.
- Start recording using MediaRecorder.start().
- Record the video.
- Stop recording using MediaRecorder.stop().
- Release the media recorder with MediaRecorder.release().
- Lock the camera using Camera.lock().
| |
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
| |
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
- Stackoverflow: MediaRecorder.stop() stop failed: -1007
- [http://developer.android.com/reference/android/media/MediaRecorder.html#stop() Developer.android.com: MediaRecorder.stop() method]
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
- android.media.MediaRecorder.AudioSource
- android.media.CamcorderProfile
- android.widget.VideoView
Favorite site
- Android developer MediaRecorder
- Develop > API Guides > Media and Camera > Audio Capture
- Analysis of Android video recording
- Android simple recording example (Audio Recorder)
- Recording and playback on Android
References
Pages that link here