Logo Icon Logo
A Crowd-sourced Cookbook on Writing Great Android® Apps
GitHub logo Twitter logo OReilly Book Cover Art

Capturing Video Using MediaRecorder

Author: Marco Dinacci -- Published? true -- FormatLanguage: W


You want to grab a video using the built-in device camera and save it to disk.


This recipe teaches the reader how to grab a video and record it on the phone by using the MediaRecorder class provided by the Android framework.


The MediaRecorder is normally used to perform audio and/or video recording. The class has a straightforward API but as it's based on a simple state machine the methods must be called in the proper order in order to avoid IllegalStateException from popping up.

Create a new Activity and override the onCreate method with the following:

    public void onCreate(Bundle savedInstanceState) {
        // we shall take the video in landscape orientation
        mSurfaceView = (SurfaceView) findViewById(R.id.surfaceView);
        mHolder = mSurfaceView.getHolder();
        mToggleButton = (ToggleButton) findViewById(R.id.toggleRecordingButton);
        mToggleButton.setOnClickListener(new OnClickListener() {
                // toggle video recording
                public void onClick(View v) {
                        if (((ToggleButton)v).isChecked())
                        else {
                                try {
                                } catch (IOException e) {

The preview frames from the camera will be displayed on a SurfaceView. Recording is controlled by a toggle button. After the recording is over, we stop the MediaRecorder. Since the stop method resets all the state machine variables in order to be able to grab another video we reset the state machine and call our initRecorder once more.

initRecorder is where we configure the MediaRecorder and the camera:

    /* Init the MediaRecorder, the order the methods are called is vital to
     * its correct functioning. 
    private void initRecorder(Surface surface) throws IOException {
        // It is very important to unlock the camera before doing setCamera
        // or it will results in a black preview
        if(mCamera == null) {
            mCamera = Camera.open();

        if(mMediaRecorder == null)
            mMediaRecorder = new MediaRecorder();
        File file = createFile();
        // No limit. Don't forget to check the space on disk.
        try {
        } catch (IllegalStateException e) {
            // This is thrown if the previous calls are not called with the 
            // proper order
        mInitSuccesful = true;

It is important to create and unlock a Camera object before of the creation of a MediaRecorder. setPreviewDisplay and setCamera must be called immediately after the creation of the MediaRecorder. The choice of the format and the output file is obligatory. Other options are optional but they must be called in the order outlined in the code above.

The MediaRecorder is best initialized when the surface has been created. We register our Activity as a SurfaceHolder.Callback listener in order to be notified of this and override the surfaceCreated method to call our initialization code:

        public void surfaceCreated(SurfaceHolder holder) {
            try {
                } catch (IOException e) {
                    // TODO Auto-generated catch block

When you're done with the surface, don't forget to release the resources, the camera is a shared object and may be used by other applications as well:

        private void shutdown() {
            // Release MediaRecorder and especially the Camera as it's a shared
            // object that can be used by other applications
            // once the objects have been released they can't be reused
            mMediaRecorder = null;
            mCamera = null;

Override the surfaceDestroyed method so the previous code can be called automatically when the user is done with the Activity:

        public void surfaceDestroyed(SurfaceHolder holder) {


The source code for this project is in the Android Cookbook repository at http://github.com/IanDarwin/Android-Cookbook-Examples, in the subdirectory MediaRecorderDemo.
nickmeinhold 2013-03-31 22:26:49.695 Thanks Ian. I ran this code on my Nexus 7 and Camera.open(); returns null. I fixed it by using Camera.open(0); but this then caused an error that was fixed by increasing the minSDKVersion to GINGERBREAD.
idarwin 2013-03-31 14:36:37.684 Nick, thanks for reporting this. I found a copy on one of the zip file cache / advertising sites, and have imported it into the github repo. The recipe link to there works.
nickmeinhold 2013-03-31 14:24:04.0 Bug: source code link on DropBox no longer exists.
Blasty 2011-05-20 17:32:18.156 Great example! Does this work in Android 2.3.3 or 2.3.4? I've tried your app in both OS versions and I only see a black preview. How do we fix this problem? Thanks!