Android MediaRecorder Stop()函数给出错误E/MediaRecorder:停止失败:-1007

Android MediaRecorder Stop()函数给出错误E/MediaRecorder:停止失败:-1007,android,camera,mediarecorder,Android,Camera,Mediarecorder,这段代码在调试模式下工作得很好,但当不是调试模式时,它总是引发运行时异常 mMediaRecorder.stop(); 根据Java文档: Stops recording. Call this after start(). Once recording is stopped, you will have to configure it again as if it has just been constructed. Note that a RuntimeException

这段代码在调试模式下工作得很好,但当不是调试模式时,它总是引发运行时异常

    mMediaRecorder.stop();
根据Java文档:

  Stops recording. Call this after start(). Once recording is stopped,
  you will have to configure it again as if it has just been constructed.
  Note that a RuntimeException is intentionally thrown to the
  application, if no valid audio/video data has been received when stop()
  is called. This happens if stop() is called immediately after
  start(). The failure lets the application take action accordingly to
  clean up the output file (delete the output file, for instance), since
  the output file is not properly constructed when this happens.

  @throws IllegalStateException if it is called before start()
这是否意味着媒体记录器未获取有效数据

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    getSupportActionBar().setTitle("Snapshot record");

    DisplayMetrics metrics = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(metrics);
    mScreenDensity = metrics.densityDpi;

    mActivity = this;

    initializeCamera();
}

private void initializeCamera() {
    if (checkCameraHardware(mActivity)) {
        mCamera = openFrontFacingCamera();
    } else {
        Toast.makeText(mActivity, "Camera not available.", Toast.LENGTH_LONG).show();
    }

    Camera.Parameters parameters = mCamera.getParameters();
    List<Camera.Size> sizes = parameters.getSupportedPreviewSizes();
    Camera.Size cs = sizes.get(sizes.size() - 1);
    parameters.setPreviewSize(cs.width, cs.height);
    mCamera.setParameters(parameters);

    mCameraPreview = new CameraPreview(this, mCamera);

    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
    preview.addView(mCameraPreview);
}

private boolean checkCameraHardware(Context context) {
    if (context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
        return true;
    } else {
        return false;
    }
}

private Camera openFrontFacingCamera() {
    int cameraCount = 0;
    Camera cam = null;
    Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
    cameraCount = Camera.getNumberOfCameras();
    for (int camIdx = 0; camIdx < cameraCount; camIdx++) {
        Camera.getCameraInfo(camIdx, cameraInfo);
        if (cameraInfo.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            try {
                cam = Camera.open(camIdx);
            } catch (RuntimeException e) {
                Log.e(TAG, "Camera failed to open: " + e.getLocalizedMessage());
            }
        }
    }

    return cam;
}


public void onSurfaceCreated() {
    startCameraRecording();
}

private void startCameraRecording() {
    if (prepareVideoRecorder()) {
        mMediaRecorder.start();
    } else {
        releaseMediaRecorder();
    }
}

private boolean prepareVideoRecorder() {
    mCamera.unlock();

    mMediaRecorder = new MediaRecorder();
    mMediaRecorder.setCamera(mCamera);
    mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
    mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
    mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
    mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
    mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
    mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString());
    mMediaRecorder.setPreviewDisplay(mCameraPreview.getHolder().getSurface());

    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;
    }
    return true;
}

protected void releaseMediaRecorder() {
    if (mMediaRecorder != null) {
        mMediaRecorder.reset();   // clear recorder configuration
        mMediaRecorder.release(); // release the recorder object
        mMediaRecorder = null;
        mCamera.lock();           // lock camera for later use

        mCamera.stopPreview();
    }
}
论方法

   protected void stopRecording() {
    try {
         if (mMediaRecorder == null)
            return;
           mMediaRecorder.stop();  // stop the recording
         } catch (RuntimeException e) {
                e.printStackTrace();
         }
         releaseMediaRecorder(); // release the MediaRecorder object
    }
堆栈跟踪:

    W/System.err: java.lang.RuntimeException: stop failed.
    W/System.err: at android.media.MediaRecorder.stop(Native Method)

您没有使用start()并直接调用
stop()


实际上,对于我来说,摄像机配置文件的尺寸集和媒体录制器的尺寸集不匹配

Camera.Parameters parameters = mCamera.getParameters();
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
profile.videoFrameWidth = optimalSize.width;
profile.videoFrameHeight = optimalSize.height;

parameters.setPreviewSize(profile.videoFrameWidth, profile.videoFrameHeight);
谢谢。

就我而言:

//step 1:
 mediarecorder.setOnErrorListener(null);  
 mediarecorder.setOnInfoListener(null);    
 mediarecorder.setPreviewDisplay(null);

 //step 2:
 mediarecorder.stop();  
如果在步骤1之前调用stop(),则会发生以下错误:


MediaRecorder.stop()stop失败:-1007

我已经开始录制了,因为我说过代码在调试模式下工作得很好。您在哪里调用startCameraRecording?我在单击按钮时调用了此方法。您能告诉我什么是
mCamera
吗?
Camera.Parameters parameters = mCamera.getParameters();
CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
profile.videoFrameWidth = optimalSize.width;
profile.videoFrameHeight = optimalSize.height;

parameters.setPreviewSize(profile.videoFrameWidth, profile.videoFrameHeight);
//step 1:
 mediarecorder.setOnErrorListener(null);  
 mediarecorder.setOnInfoListener(null);    
 mediarecorder.setPreviewDisplay(null);

 //step 2:
 mediarecorder.stop();