Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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();
like image 907
Anurag Ramdasan Avatar asked Mar 11 '12 18:03

Anurag Ramdasan


2 Answers

The reason for such behavior is that you are probably (95% sure) calling recorder.setOutputFile() again after you have finished your recording (after recorder.stop() perhaps). Thus old file is being rewritten by new empty file.

like image 140
Alex Semeniuk Avatar answered Nov 20 '22 20:11

Alex Semeniuk


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

like image 4
Anurag Ramdasan Avatar answered Nov 20 '22 19:11

Anurag Ramdasan