Android: 设置录制视频大小的正确且通用的方法是什么?

Android: What is the right and universal way to set the video size for recording?

我正在开发用于 android 媒体录制的应用程序,但某些设备的屏幕录制分辨率不同。它总是让我崩溃。

我正在使用 google 正式使用的代码 https://github.com/googlesamples/android-MediaRecorder/blob/master/Application/src/main/java/com/example/android/mediarecorder/MainActivity.java

它也不适用于 Sony Xperia Z2 D6503

在我调查这段代码后

public static Camera.Size getOptimalVideoSize(List<Camera.Size> supportedVideoSizes,
                                                  List<Camera.Size> previewSizes, int w, int h)

无法正常工作,因为它会导致尺寸为 1280x720

日志显示

08-18 08:30:12.367 13251 13251 D MediaRecorder: Using Profile 0
08-18 08:30:12.367 13251 13251 D CameraTexture: vidsize = 720 : 1280

08-18 08:30:12.369   339  1370 W ServiceManager: Permission failure: com.sonyericsson.permission.CAMERA_EXTENDED from uid=19621 pid=13251
08-18 08:30:12.370   339  1370 W ServiceManager: Permission failure: com.sonyericsson.permission.CAMERA_EXTENDED from uid=19621 pid=13251
08-18 08:30:12.377  1478  1478 I art     : Explicit concurrent mark sweep GC freed 21(1200B) AllocSpace objects, 0(0B) LOS objects, 40% free, 12MB/21MB, paused 806us total 23.539ms
08-18 08:30:12.390 13251 13251 E MediaRecorder: Starting Record
08-18 08:30:12.391   339   974 W StagefrightRecorder: Intended video encoding frame width (1280) is too large and will be set to (800)
08-18 08:30:12.392   339   974 W StagefrightRecorder: Intended video encoding frame height (720) is too large and will be set to (480)
08-18 08:30:12.393   339   974 E CameraSource: Video dimension (800x480) is unsupported
08-18 08:30:12.394 13251 13251 E MediaRecorder: start failed: -19

我不知道如何正确创建可在所有 android 设备上使用的媒体记录器。这个有参考吗?

如果您只对 API 21+ 感兴趣,您可以使用 getVideoCapabilities() 方法。

CamcorderProfile class 自 API 8 以来就已存在,但没那么有用。

如果所有其他方法都失败了,请反复试验,测试每个所需的解决方案并标记那些不起作用的解决方案。

(原来是 here

编辑: 使用 CamcorderProfile 你应该在你的情况下使用 QUALITY_HIGH(我想阅读你的代码示例)