Android相机视频录制问题

时间:2018-04-05 06:33:43

标签: android camera android-camera2 recording video-recording

我创建了一个项目,我正在录制视频。当我在Android 8.1.0中运行项目时,会出现以下错误。  android "mediarecorder: start failed: -19"上的recorder.start()

我正在使用代码

private void startRecording() {

    try {

        if (checkAvailabity()) {
            flashOn();
            findViewById(R.id.btn_fail).setVisibility(View.VISIBLE);
            prepareRecorder();
            isRecordVideoClicked = true;
            isPriviewVideoClicked = false;
            enablePassButton();
            mThumbnail.setVisibility(View.GONE);
            timerValue = (TextView) findViewById(R.id.timer);
            filePathLabel.setVisibility(View.GONE);
            filePathValue.setText("");

            recordingIndicator.setVisibility(View.VISIBLE);

            startTime = 0L;

            timeInMilliseconds = 0L;

            timeSwapBuff = 0L;

            updatedTime = 0L;
            startTime = SystemClock.uptimeMillis();
            customHandler.postDelayed(updateTimerThread, 0);
            recording = true;

            recorder.start();
            start.setEnabled(false);
            start.setBackgroundResource(R.drawable.button_disabled);

            AppLogger.v(this, TAG, "Recording Started");
        } else {
            findViewById(R.id.btn_pass).setEnabled(false);
        }

    } catch (Exception e) {
        mThumbnail.setVisibility(View.GONE);
        timerValue.setVisibility(View.GONE);
        filePathLabel.setVisibility(View.GONE);
        recordingIndicator.setVisibility(View.GONE);
        Toast.makeText(CameraVideoTestActivity.this, R.string.error_on_video_recording, Toast.LENGTH_LONG).show();
         }
}

private void prepareRecorder() {
    try {
        int rotation = getWindowManager().getDefaultDisplay().getRotation();
        recorder = new MediaRecorder();
        //recorder.setVideoSize(320, 240);
        recorder.setPreviewDisplay(holder.getSurface());
        if (cameraFront) {
            CameraInfo info = new CameraInfo();
            rotation = (info.orientation - 90 + 360) % 360;
            recorder.setOrientationHint(rotation);
        } else {
            recorder.setOrientationHint(ORIENTATIONS.get(rotation));
        }

        if (usecamera) {
            camera.lock();
            camera.unlock();
            recorder.setCamera(camera);

        }
        recorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
        recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);

        recorder.setProfile(camcorderProfile);

        if (camcorderProfile.fileFormat == MediaRecorder.OutputFormat.THREE_GPP) {
            createthreeGppFile();
        } else {
            createOtherFormateVideo();
        }
        recorder.prepare();

    } catch (Exception e) {

        setTestPerformed(true);
        supportInvalidateOptionsMenu();

        AppLogger.exception(this, getClass().getSimpleName(), e);
    }
}

我正在使用以下代码准备相机

 private void prepareCamera() throws Exception {
    parameters = camera.getParameters();

    List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
    Camera.Size previewSize = previewSizes.get(0);
    parameters.setPreviewSize(previewSize.width, previewSize.height);
    parameters.setPreviewFrameRate(camcorderProfile.videoFrameRate);

    camera.setParameters(parameters);

    camera.setPreviewDisplay(holder);
    camera.setDisplayOrientation(90);
    camera.startPreview();
    previewRunning = true;

}

在onSurface中使用以下代码

创建
@Override
public void surfaceCreated(SurfaceHolder holder) {
    try {
        AppLogger.v(this, TAG, "surfaceCreated");

        if (usecamera) {
            releaseCameraAndPreview();
            if (cameraFront) {
                if (camcorderProfile.fileFormat == MediaRecorder.OutputFormat.THREE_GPP) {
                    camcorderProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_LOW);
                } else {
                    camcorderProfile = CamcorderProfile.get(findFrontFacingCamera(), CamcorderProfile.QUALITY_LOW);
                }
                camera = Camera.open(findFrontFacingCamera());

            } else {
                camcorderProfile = CamcorderProfile.get(findBackFacingCamera(), CamcorderProfile.QUALITY_HIGH);
                camera = Camera.open(findBackFacingCamera());
            }
            if (camera == null) {
                start.setEnabled(false);
                start.setBackgroundResource(R.drawable.button_disabled);
                setTestPerformed(true);
                supportInvalidateOptionsMenu();
            } else {
                start.setEnabled(true);
                start.setBackgroundResource(R.drawable.shadow);
                start.setPadding(20, 0, 20, 0);
            }
        }
    } catch (Exception e) {
        AppLogger.exception(this, getClass().getSimpleName(), e);
    }

}

请提供一些解决方案。谢谢

1 个答案:

答案 0 :(得分:0)

花了1天后我得到了解决方案,只需更改下面的代码,现在一切正常。

 recorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_720P));