Search code examples
androidvideo-recordingandroid-mediarecorder

android mediarecoder saves empty file


This is the code i used to record video from an android device in MP4 format. The file is being created but is of 0 bytes size. I dont seem to understand what has gone wrong. Any help will be appreciated.

                if(mCamera == null) {
        mCamera = Camera.open();
        mCamera.unlock();
    }

    if(mediaRecorder == null)
        mediaRecorder = new MediaRecorder();


    mediaRecorder.setCamera(mCamera);

    mediaRecorder.setCamera(mCamera);
    mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
    mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

    mediaRecorder.setMaxDuration(maxDurationInMs);


    mediaRecorder.setOutputFile("/sdcard/1.mp4");

    mediaRecorder.setVideoFrameRate(videoFramesPerSecond);
    mediaRecorder.setVideoSize(176,144);

    mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);

    mediaRecorder.setPreviewDisplay(surface);

    mediaRecorder.setMaxFileSize(maxFileSizeInBytes);

                mediaRecorder.prepare();        
    try {
        mediaRecorder.prepare();
    } catch (IllegalStateException e) {
        // This is thrown if the previous calls are not called with the 
        // proper order
        e.printStackTrace();
    }

            mediaRecorder.start();

Solution

  • okay so i finally figured it out myself. i used the method described here and it worked properly.

    http://developer.android.com/guide/topics/media/camera.html#saving-media