如何在两个不同的设备上使用Media Recorder android获得固定的视频大小?

时间:2015-04-26 19:11:00

标签: android android-camera mediarecorder android-mediarecorder

我正在尝试阅读有关媒体录制器的更多信息。视频大小在不同设备中会有所不同。在一台设备中,视频大小约为31 MB,另一台则为88.5 MB。

以下是我用于媒体录制器的代码。

recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
recorder.setProfile(camcorderProfile);
recorder.setVideoSize(optimalVideoSize.width, optimalVideoSize.height);
recorder.setVideoEncodingBitRate(1000000);
recorder.setPreviewDisplay(holder.getSurface());
recorder.setMaxDuration(time_limit*1000);   

其中,optimalvideosize是使用以下函数输出的arraylist:

private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) 
{
final double ASPECT_TOLERANCE = 0.2;
double targetRatio = (double) w / h;
if (sizes == null)
    return null;

Size optimalSize = null;
double minDiff = Double.MAX_VALUE;

int targetHeight = h;

// Try to find an size match aspect ratio and size
for (Size size : sizes) 
{
    Log.d("Camera", "Checking size " + size.width + "w " + size.height
            + "h");
    double ratio = (double) size.width / size.height;
    if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE)
        continue;
    if (Math.abs(size.height - targetHeight) < minDiff) 
    {
        optimalSize = size;
        minDiff = Math.abs(size.height - targetHeight);
    }
}

// Cannot find the one match the aspect ratio, ignore the
// requirement
if (optimalSize == null) 
{
    minDiff = Double.MAX_VALUE;
    for (Size size : sizes) 
    {
        if (Math.abs(size.height - targetHeight) < minDiff) 
        {
            optimalSize = size;
            minDiff = Math.abs(size.height - targetHeight);
        }
    }
}
return optimalSize;
}

感谢帮助!!

0 个答案:

没有答案