Not able to set manual video size in Android MediaRecorder

[亡魂溺海] 提交于 2021-02-19 06:09:54

问题


i am using MediaRecorder in Android to record a video, mu current paramaters are:

    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
    mMediaRecorder.setOutputFile(getOutputMediaFile().toString());
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

    mMediaRecorder.setVideoEncodingBitRate(2500000);
    mMediaRecorder.setVideoSize(640, 480);

These parameters are supported in all the devices i have tried (4.0 and above), but if i change the last line of the code to:

mMediaRecorder.setVideoSize(960, 540);

Then an error occurs:

java.lang.RuntimeException: start failed.
android.media.MediaRecorder.start(Native Method)

Can anybody help, as i want to record the video in 960x540 resolution.


回答1:


Use this code this will help:

try {
            recording = true;
            mrec = new MediaRecorder();
            mCamera.unlock();
            mrec.setCamera(mCamera);

            //Set audio source
            mrec.setAudioSource(MediaRecorder.AudioSource.MIC);
            //set video source
            mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA);

            //set output format
            mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);

            int width = 0;//set whatever 
            int height = 0;//set whatever
            try {
                //get the available sizes of the video
                List<Size> tmpList = getSupportedVideoSizes();

                final List<Size> sizeList = new Vector<Size>();

                // compare the apsect ratio of the candidate sizes against the
                // real ratio
                Double aspectRatio = (Double.valueOf(getWindowManager()
                        .getDefaultDisplay().getHeight()) / getWindowManager()
                        .getDefaultDisplay().getWidth());
                for (int i = tmpList.size() - 1; i > 0; i--) {
                    Double tmpRatio = Double.valueOf(tmpList.get(i).height)
                            / tmpList.get(i).width;
                    if (EnableLog.LOG_TAG) {
                        Log.e("Width & height", tmpList.get(i).width + " x "
                                + tmpList.get(i).height);
                    }
                    if (Math.abs(aspectRatio - tmpRatio) < .15) {
                        width = tmpList.get(i).width;
                        height = tmpList.get(i).height;
                        sizeList.add(tmpList.get(i));
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            // set the size of video.
            // If the size is not applicable then throw the media recorder stop
            // -19 error
            mrec.setVideoSize(width, height);

            // Set the video encoding bit rate this changes for the high, low.
            // medium quality devices
            mrec.setVideoEncodingBitRate(1700000);

            //Set the video frame rate
            mrec.setVideoFrameRate(30);

            //set audio encoder format
            mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);

            //set video encoder format
            mrec.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

            //Show the display preview
            mrec.setPreviewDisplay(surfaceHolder.getSurface());

            //output file path
            mrec.setOutputFile(output_path);

            mrec.prepare();

            mrec.start();

        } catch (IllegalStateException e) {

            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }

And this too :

public List<Size> getSupportedVideoSizes() {
        if (params.getSupportedVideoSizes() != null) {
            return params.getSupportedVideoSizes();
        } else {
            // Video sizes may be null, which indicates that all the supported
            // preview sizes are supported for video recording.
            return params.getSupportedPreviewSizes();
        }
    }

I hope this will solve your problem.

NOTE

After updating 4.4.2 getPreviewsizes() not work in Samsung Galaxy Tab 3(7") (For me). So i hope preview sizes not work in all the devices . So check the video sizes first and if returns sizes then use it or if it returns null then use getPreviewsizes that is in my code.

And i guess you got another error message like Media recorder start failed error -19 ? is it?



来源:https://stackoverflow.com/questions/27058579/not-able-to-set-manual-video-size-in-android-mediarecorder

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!