从Android中的相机捕捉时间流逝

时间:2017-07-17 04:59:48

标签: java android video

我想从相机捕捉时间流逝。我已经处理了视频录制,但它运行良好。但是当我尝试录制时间推移视频时,它就出错了。

这是我正在尝试的代码:

mMediaRecorder = new MediaRecorder();

    // Step 1: Unlock and set camera to MediaRecorder
    mCamera.unlock();
    mMediaRecorder.setCamera(mCamera);

    // Step 2: Set sources
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

    // Step 3: Set a CamcorderProfile (requires API Level 8 or higher)
    mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_TIME_LAPSE_HIGH));

    // Step 4: Set output file
    mMediaRecorder.setOutputFile(String.valueOf(GeneralUtils.getOutputMediaFile(MEDIA_TYPE_VIDEO)));

    // Step 5: Set the preview output
    mMediaRecorder.setPreviewDisplay(this.getHolder().getSurface());


    // Step 6: Prepare configured MediaRecorder
    try {
        mMediaRecorder.prepare();
    } catch (IllegalStateException e) {
        Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    } catch (IOException e) {
        Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage());
        releaseMediaRecorder();
        return false;
    }

在我查看视频后录制视频,其大小为0.00B,无法播放此视频。为什么这不录制视频。

我尝试了更多内容,并发现如果您将CamcorderProfile.QUALITY_TIME_LAPSE_HIGH替换为CamcorderProfile.QUALITY_HIGH,它就会开始工作。我搜索了这个,发现原因可能是你的设备不支持QUALITY_TIME_LAPSE_HIGH。但是,当我打开原生相机应用程序并点击记录时间推移视频时,这是奇怪的事情,它正常工作,这意味着我的设备支持QUALITY_TIME_LAPSE_HIGH

我的问题是为什么它不能用于我的应用程序。我的代码中有错误吗?能不能让我知道。

1 个答案:

答案 0 :(得分:0)

这项工作在我的应用上。

                        mCamera.unlock();
                    if(videoRecorder == null) videoRecorder = new MediaRecorder();
                    videoRecorder.setCamera(mCamera);
                    videoRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
                    videoRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
                    if(timeLapsedInterval == 0){
                        videoRecorder.setProfile(CamcorderProfile.get(cameraUsing, CamcorderProfile.QUALITY_HIGH));
                    }else{
                        videoRecorder.setProfile(CamcorderProfile.get(cameraUsing, CamcorderProfile.QUALITY_TIME_LAPSE_HIGH));
                        videoRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.DEFAULT);
                    }

                    android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
                    android.hardware.Camera.getCameraInfo(cameraUsing, info);
                    int orientation = (rt + 45) / 90 * 90;
                    int rotation = 0;
                    if (info.facing == CameraInfo.CAMERA_FACING_FRONT) {
                        rotation = (info.orientation - orientation + 360) % 360;
                    } else {  // back-facing camera
                        rotation = (info.orientation + orientation) % 360;
                    }
                    videoRecorder.setOrientationHint(rotation);

                    String df = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());
                    videoFile = new File(dir, df+".mp4");
                    videoRecorder.setOutputFile(videoFile.getAbsolutePath());
                    videoRecorder.setPreviewDisplay(sh.getSurface());
                    mrlOverall.setBackgroundColor(Color.WHITE);

                    if(timeLapsedInterval != 0) videoRecorder.setCaptureRate(1f/timeLapsedInterval); 

希望有所帮助!