开发者社区> 问答> 正文

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

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(),停止录像。

展开
收起
爵霸 2016-06-12 13:09:47 3207 0
0 条回答
写回答
取消 提交回答
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载