Android camera录制视频没有preview为什么不能成功?API21-问答-阿里云开发者社区-阿里云

开发者社区> 问答> 正文

Android camera录制视频没有preview为什么不能成功?API21

爵霸 2016-06-12 13:09:47 2320

http://developer.android.com/guide/topics/media/camera.html

Note: It is possible to use MediaRecorder without creating a camera preview first and skip the first few steps of this process. However, since users typically prefer to see a preview before starting a recording, that process is not discussed here.

Android的文档如上,我想不创建一个照相机的预览界面,按理来说是可行的。我参考文档里的步骤,省去startpreview.核心代码如下

  public void init(){
        CameraManager manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
        Log.e(TAG, "OpenCamera-Video E");
        String cameraId = null;
        try {
            if(!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)){
                throw  new RuntimeException("Time out waiting to lock Camera opening");
            }
             cameraId = manager.getCameraIdList()[0];
             CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
             StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
            mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class));
            mMediaRecorder = new MediaRecorder();
            manager.openCamera(cameraId,mStateCallback,null);
            setUpMediaRecorder();

        } catch (CameraAccessException e) {
            e.printStackTrace();
            Log.e(TAG,"cannot access the camera");
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        Log.e(TAG,"openCamera-video X");
                }

                    private void setUpMediaRecorder() throws IOException {

        mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
        mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
        mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
        mMediaRecorder.setVideoEncodingBitRate(10000000);
        mMediaRecorder.setVideoFrameRate(30);
        mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
        mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
        mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);

        mMediaRecorder.setOutputFile(String.valueOf(getVideoFile()));
        //mMediaRecorder.setOrientationHint(orientation);
        mMediaRecorder.prepare();
    }


          private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
        @Override
        public void onOpened(CameraDevice cameraDevice) {
            mCameraDevice = cameraDevice;
            mCameraOpenCloseLock.release();
        }

        @Override
        public void onDisconnected(CameraDevice cameraDevice) {
            mCameraOpenCloseLock.release();
            cameraDevice.close();
            mCameraDevice = null;
        }

        @Override
        public void onError(CameraDevice cameraDevice, int i) {
            mCameraOpenCloseLock.release();
            cameraDevice.close();
            mCameraDevice = null;
        }
    };


   private void startRecordingVideo() {
        try {
            // UI
       //      mButtonVideo.setText(R.string.stop);
            mIsRecordingVideo = true;
            Log.i("VideoService","在startRecordingVideo里");
            // Start recording
            mMediaRecorder.start();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        }
    }

    private  boolean mIsRecordingVideo = false;

    private void stopRecordingVideo() {
        // UI
        mIsRecordingVideo = false;
       // mButtonVideo.setText(R.string.record);
        // Stop recording
        mMediaRecorder.stop();
        mMediaRecorder.reset();
        mMediaRecorder.release();
       // Activity activity = getActivity();
            Toast.makeText(this, "Video saved: " + getVideoFile(),
                    Toast.LENGTH_SHORT).show();

       // startPreview();
    }

在初始化的时候执行init()函数,之后,点击button按钮,对mIsRecordingVideo值判断,若为false,进入startRecordingVideo(),启动录像;若为true,进入stopRecordingVideo(),停止录像。

api视频 vip什么 android录制 view视频 camera功能
分享到
取消 提交回答
全部回答(0)
开发与运维
使用钉钉扫一扫加入圈子
+ 订阅

集结各类场景实战经验,助你开发运维畅行无忧

相似问题
最新问题
推荐课程