android – 使用camera2和MediaRecorder捕获视频

前端之家收集整理的这篇文章主要介绍了android – 使用camera2和MediaRecorder捕获视频前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我尝试一次录制2种不同分辨率的视频.
有时一切顺利,有时应用程序似乎陷入2 MediaRecorders的stop()方法.有人可以查看我的代码,我做错了吗?

package com.example.android.videorecording;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;

import android.hardware.camera2.params.StreamConfigurationMap;
import android.media.MediaCodec;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.hardware.camera2.*;
import android.os.Environment;
import android.util.Size;
import android.view.Surface;
import android.view.View;
import android.widget.Button;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class MainActivity extends Activity {
    CameraDevice mCamera;
    MediaRecorder mMediaRecorderLow = new MediaRecorder();
    MediaRecorder mMediaRecorderHigh = new MediaRecorder();
    CaptureRequest mCaptureRequest;
    CameraCaptureSession mSession;
    boolean recording = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        CameraManager manager = (CameraManager) getSystemService(CAMERA_SERVICE);



        File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),"Test");
        if(!dir.exists()) {
            boolean s = dir.mkdirs();
            if(!s) {
                AlertDialog.Builder alert = new AlertDialog.Builder(this);
                alert.setMessage("could not create dir");
                alert.show();
            }
        }
        final File outputFileLow = new File(dir.getAbsolutePath(),"testLow.mp4");
        final File outputFileHigh = new File(dir.getAbsolutePath(),"testHigh.mp4");


        try {
            outputFileLow.createNewFile();
            String[] cameras = manager.getCameraIdList();
            CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameras[0]);
            StreamConfigurationMap configs = characteristics.get(
                    CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            Size[] sizes = configs.getOutputSizes(MediaCodec.class);
            final Size sizeLow = sizes[5];
            final Size sizeHigh = sizes[0];
            manager.openCamera(cameras[0],new CameraDevice.StateCallback() {
                @Override
                public void onOpened(CameraDevice camera) {
                    mCamera = camera;


                    mMediaRecorderLow.setVideoSource(MediaRecorder.VideoSource.SURFACE);
                    mMediaRecorderLow.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                    mMediaRecorderLow.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
                    mMediaRecorderLow.setVideoSize(sizeLow.getWidth(),sizeLow.getHeight());
                    mMediaRecorderLow.setMaxFileSize(0);
                    mMediaRecorderLow.setOrientationHint(0);

                    mMediaRecorderHigh.setVideoSource(MediaRecorder.VideoSource.SURFACE);
                    mMediaRecorderHigh.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
                    mMediaRecorderHigh.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
                    mMediaRecorderHigh.setVideoSize(sizeHigh.getWidth(),sizeHigh.getHeight());
                    mMediaRecorderHigh.setMaxFileSize(0);
                    mMediaRecorderLow.setOrientationHint(0);



                    try {
                        mMediaRecorderLow.setOutputFile(outputFileLow.getAbsolutePath());
                        mMediaRecorderHigh.setOutputFile(outputFileHigh.getAbsolutePath());
                        mMediaRecorderLow.prepare();
                        mMediaRecorderHigh.prepare();
                        ListFailed(CameraCaptureSession session) {
                                mSession = session;
                            }
                        },null);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                @Override
                public void onDisconnected(CameraDevice camera) {

                }

                @Override
                public void onError(CameraDevice camera,int error) {

                }
            },null);

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


    }

    /**
     * is called by button,should start/stop recording
     * @param v
     */
    public void trigger(View v) {


                ((Button)findViewById(R.id.button)).setText("stop");      try {
            if(!recording) {
                mMediaRecorderLow.start();
                mMediaRecorderHigh.start();
                mSession.setRepeatingRequest(mCaptureRequest,new CameraCaptureSession.CaptureCallback() {
                            @Override
                            public void onCaptureStarted(CameraCaptureSession session,CaptureRequest request,long timestamp,long frameNumber) {
                                super.onCaptureStarted(session,request,timestamp,frameNumber);
                            }

                            @Override
                            public void onCaptureCompleted(CameraCaptureSession session,TotalCaptureResult result) {
                                super.onCaptureCompleted(session,result);
                            }
                        },null);

                ((Button)findViewById(R.id.button)).setText("stop");
                recording = true;
            } else {
                ((Button)findViewById(R.id.button)).setText("start");
                recording = false;
                mSession.stopRepeating();
                mMediaRecorderHigh.stop();
                mMediaRecorderLow.stop();
            }
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
    }

}
最佳答案
您还需要在停止后释放MediaRecorder.名称为低设置.请在Trigger方法的其他部分中尝试以下代码.

    mMediaRecorderHigh.stop();
    mMediaRecorderHigh.reset();
    mMediaRecorderHigh.release();
    mCamera.release();
    mMediaRecorderHigh= null;

猜你在找的Android相关文章