当getSupportedVideoSizes返回null时,如何设置VideoSize

时间:2016-08-04 07:10:31

标签: android mediarecorder

当我使用MediaRecorder录制视频时,我想设置视频大小。但是相机getParameters()返回getSupportedVideoSizes为null并且getSupportedPreviewSizes很好,它返回1920x1088,1280x960,1280x720,960x540,720x540,720x480 ,640x480,352x288,320x240,176x144。但是,如果我设置MediaRecorder的值videoSize,当我开始记录时,绑定的SurfaceView屏幕会在几秒钟后被冻结。所以,我该怎么办?

2 个答案:

答案 0 :(得分:1)

我最近的解决方法就是:

    public void setVideoSize(int width, int height) {
    // Just set Preview size...
    Camera.Parameters params = getParameters();
    List<Camera.Size> sizes = params.getSupportedPreviewSizes();
    Camera.Size preferred = params.getPreferredPreviewSizeForVideo();
    if (preferred == null) {
        preferred = sizes.get(0);
    }

    Camera.Size optimalPreview = Util.getOptimalPreviewSize(mContext, sizes,
            width, height);
    setPreviewSize(optimalPreview.width, optimalPreview.height);
}

Util.getOptimalPreviewSize是:

/**
     * Returns the optimal preview size for photo shots
     *
     * @param currentActivity
     * @param sizes
     * @param widthImage
     * @param heightImage
     * @return
     */
    public static Size getOptimalPreviewSize(Activity currentActivity,
                                             List<Size> sizes, int widthImage, int heightImage) {
        final double ASPECT_TOLERANCE = 0.05;
        double targetRatio = 0;

        if (sizes == null) return null;

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

        // Recupero le dimensioni dello schermo per confrontare le dimensioni della
        // preview con quelle dello schermo, in modo tale che a video veda esattamente ciò che poi
        // andrò a fotografare
        int widthScreen = currentActivity.getWindow().getDecorView().getWidth();
        int heightScreen = currentActivity.getWindow().getDecorView().getHeight();

        targetRatio = (double) widthImage / heightImage;

        if (Util.getDisplayRotation(currentActivity) == 0 || Util.getDisplayRotation(currentActivity) == 180) {
            int temp = heightScreen;
            heightScreen = widthScreen;
            widthScreen = temp;
        }

        int targetHeight = heightScreen;

        for (Camera.Size size : sizes) {
            double ratio = (double) size.width / size.height;
            if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue;

            if (Math.abs(size.height - targetHeight) < minDiff) {
                if (size.width <= widthScreen && size.height <= heightScreen) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }

        if (optimalSize == null) {
            minDiff = Double.MAX_VALUE;
            for (Camera.Size size : sizes) {
                if (Math.abs(size.height - targetHeight) < minDiff) {
                    optimalSize = size;
                    minDiff = Math.abs(size.height - targetHeight);
                }
            }
        }
        return optimalSize;
    }

根据相机的旋转(不是屏幕!!)和图像/视频的尺寸来捕捉它会找到最佳的视频/图像预览尺寸

答案 1 :(得分:0)

最后我解决了这个问题,预览textureview在我的活动的顶部,还有一些其他的。我在java代码中设置了textureview的宽度和高度,但是如果我用dp在XML中设置它,一切都还可以,那么,预览纹理视图(宽度和高度)和之间的关系是什么?录制视频(宽度和高度)