Search code examples
androidandroid-camera2android-7.0-nougatandroid-mediarecorder

Front cam recording using MediaRecorder not working smoothly


I have a Huawei P9 Plus smartphone running Android 7.0. I'm using MediaRecorder to record the front cam. It is a 8 MP camera. I'm using the following settings (I think this is the most important part, I'm not posting the whole class because it is too much lines of code):

mMediaRecorder = new MediaRecorder();
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4)
mMediaRecorder.setOutputFile(videoFile.getAbsolutePath());
mMediaRecorder.setVideoEncodingBitRate(8000000);
mMediaRecorder.setVideoFrameRate(30)
mMediaRecorder.setVideoSize(1024 , 1920)
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaSurface = MediaCodec.createPersistentInputSurface();
mMediaRecorder.setInputSurface(mMediaSurface);
mMediaRecorder.prepare();

With this settings it works but sometimes the video is a bit jerky. Strange is also that with video size 1024 x 1920 it works but when I set 1080 x 1920 it does not work anymore (there is no error but the video is completely corrupted). Why is that? In the supported resolutions I got from the front cam characteristics 1080 x 1920 is listed but not 1024 x 1920.

Are my other settings ok? Is setVideoEncodingBitRate ok for a 8 MP camera?

I have also tried to use a given profile as follows:

mMediaRecorder = new MediaRecorder();
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFile(videoFile.getAbsolutePath());        
mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_1080P));
mMediaRecorder.setVideoFrameRate(30)
mMediaSurface = MediaCodec.createPersistentInputSurface();
mMediaRecorder.setInputSurface(mMediaSurface);
mMediaRecorder.prepare();

When I run it this way I'm getting an error when I try to stop MediaRecorder (stop failed: -1007), probably because starting video recording did not succeed. Why? Did I make a mistake?


Solution

  • When I run it this way I'm getting an error when I try to stop MediaRecorder (stop failed: -1007), probably because starting video recording did not succeed. Why? Did I make a mistake

    Your probably right. Try checking or adding property(e.g Boolean) to determine if the recorder has already started. In that case you won't be able to call stop if it is not yet started.