Search code examples
androidvideoandroid-cameramediarecorderaspect-ratio

MediaRecorder fails even when matching Video Size and Preview Size aspect ratios


According to the Camera.Parameters#getPreferredSizeForVideoPreview documentation:

we recommend to choose a preview size that has the same aspect ratio as the resolution of video to be recorded

I have found that, on certain devices (Galaxy S3), that selecting a video recording size with a different aspect ratio does indeed cause problems recording video (green/purple videos).

When I follow the documentation's advice and stick to the same aspect ratio, it mostly works, but on some devices (Nexus S / Android 4.0.4), calls to MediaRecorder.start() fail with the message:

E/MediaRecorder﹕ start failed: -19

On other devices (HTCEVOV4G / Android 4.0.3) calls to MediaRecorder.stop fail with the message:

E/MediaRecorder﹕ stop failed: -1007

I do find, however, that if I call MediaRecorder#setVideoSize with the exact same dimensions as the camera's preview size, the video recording works.


Solution

  • The devices that have shown this problem seem to have one thing in common. Calls to Camera.Parameters#getSupportedVideoSizes() all return null! According to the documentation for getSupportedVideoSizes, the function will return:

    Returns
    a list of Size object if camera has separate preview and video output; otherwise, null is returned.

    This means that, for these devices, the camera's preview and video output are identical, so setting different sizes, no matter the aspect ratio, is going to cause problems.

    This can be fixed by using the same dimensions for both the camera preview size (Camera.Parameters#setPreviewSize) and the video recording size (MediaRecorder#setVideoSize).

    Something to note, however, is that not all devices that use the same preview and and video outputs suffer from this issue. The HTC One Mini (Android 4.4.2), for example, will change its preview resolution on the fly to match the video recording resolution. While not confirmed, my testing leads me to believe that this is an issue when using a SurfaceView as a surface for recording as opposed to using a TextureView. Pre-4.1, it seems accepted that recording with a TextureView was not supported, so ICS and below you need to make sure your preview and video sizes match, but in JB+, it seems to not matter. This could be heavily device dependent, and I have not confirmed this theory.

    Ah yes, the wild world of android!