MediaRecorder可以保存与SurfaceView相同大小的视频

时间:2016-09-25 18:04:33

标签: android android-camera android-mediarecorder

你好我想在我的应用程序中实现录制。我能够录制视频并保存,但是方面的速率和分辨率完全搞砸了。如何以最高质量注册?

这是我的代码

public class VideoFragment extends Fragment implements SurfaceHolder.Callback{
    private ToggleButton mToggleButton;
    private SurfaceHolder mHolder;
    private SurfaceView mSurfaceView;
    private MediaRecorder mMediaRecorder;
    private Camera mCamera;
    private boolean mInitSuccesful;

    public static VideoFragment newInstance() {
        Bundle args = new Bundle();
        VideoFragment fragment = new VideoFragment();
        fragment.setArguments(args);
        return fragment;
    }

    public VideoFragment(){}

    @Override
    public void onStart() {
        super.onStart();
        getActivity().setTitle(R.string.video);
    }

    @Override
    public void onResume() {
        super.onResume();


    }

    @Override
    public void onPause() {
        super.onPause();
        shutdown();
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_content, container, false);

        Button yo = (Button) rootView.findViewById(R.id.pic);
        yo.setVisibility(View.GONE);


        mSurfaceView = (SurfaceView) rootView.findViewById(R.id.cameraView);
        mHolder = mSurfaceView.getHolder();
        mHolder.addCallback(this);
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        mToggleButton = (ToggleButton) rootView.findViewById(R.id.toggleRecordingButton);
        mToggleButton.setOnClickListener(new View.OnClickListener() {
            @Override
            // toggle video recording
            public void onClick(View v) {

                if (((ToggleButton)v).isChecked())
                    if(mMediaRecorder == null) {
                        try {
                            initRecorder(mHolder.getSurface());
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }else{
                        try {
                            mMediaRecorder.start();
                        }catch (NullPointerException e){
                            e.printStackTrace();
                            try {
                                initRecorder(mHolder.getSurface());
                            } catch (IOException ex) {
                                ex.printStackTrace();
                            }
                        }
                    }
                else {
                    mMediaRecorder.stop();
                    mMediaRecorder.reset();
                    try {
                        initRecorder(mHolder.getSurface());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }


            }
        });

        return rootView;
    }

    /* Init the MediaRecorder, the order the methods are called is vital to
     * its correct functioning.
     */
    private void initRecorder(Surface surface) throws IOException {
        // It is very important to unlock the camera before doing setCamera
        // or it will results in a black preview
        if(mCamera == null) {
            mCamera = Camera.open();
            mCamera.unlock();
        }

        if(mMediaRecorder == null)
            mMediaRecorder = new MediaRecorder();

        mMediaRecorder.setPreviewDisplay(surface);
        mMediaRecorder.setCamera(mCamera);

        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
        File file = new File(Environment
                .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),
                Utils.getVideoFilename());
        // "touch" the file
        if(!file.exists()) {
            File parent = file.getParentFile();
            if(parent != null)
                if(!parent.exists())
                    if(!parent.mkdirs())
                        throw new IOException("Cannot create " +
                                "parent directories for file: " + file);

            file.createNewFile();
        }

        mMediaRecorder.setOutputFile(file.getAbsolutePath());

        // No limit. Don't forget to check the space on disk.
        mMediaRecorder.setMaxDuration(-1);
        mMediaRecorder.setVideoFrameRate(15);

        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.DEFAULT);

        try {
            mMediaRecorder.prepare();
        } catch (IllegalStateException e) {
            // This is thrown if the previous calls are not called with the
            // proper order
            e.printStackTrace();
        }

        mInitSuccesful = true;
    }

    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        try {
            if(!mInitSuccesful)
                initRecorder(mHolder.getSurface());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        shutdown();
    }



    private void shutdown() {
        // Release MediaRecorder and especially the Camera as it's a shared
        // object that can be used by other applications
        mMediaRecorder.reset();
        mMediaRecorder.release();
        mCamera.release();

        // once the objects have been released they can't be reused
        mMediaRecorder = null;
        mCamera = null;
    }
}

由于

修改 解决方案归功于已接受的答案

CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
mMediaRecorder.setProfile(cpHigh);

1 个答案:

答案 0 :(得分:0)

首先尝试使用CamcorderProfile设置所有MediaRecorder分辨率和质量设置;您当前没有设置许多值,例如大小。 MediaRecorder将不会关注SurfaceView的尺寸。