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

时间:2016-03-18 07:34:59

标签: android camera mediarecorder

这段代码在调试模式下运行良好,但如果不是调试模式,它总是抛出运行时异常

    mMediaRecorder.stop();

根据Java doc:

  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();
    }
}

CameraPreview类

public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "CameraPreview";
    private SurfaceHolder mHolder;
    private Camera mCamera;
    Activity activity;

    public CameraPreview(Activity context, Camera camera) {
        super(context);
        mCamera = camera;
        activity = context;
        mHolder = getHolder();
        mHolder.addCallback(this);

        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    public void surfaceCreated(SurfaceHolder holder) {
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();

            if (activity instanceof SnapShotActivity)
                ((SnapShotActivity) activity).onSurfaceCreated();

        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null) {
            // preview surface does not exist
            return;
        }

        // stop preview before making changes
        try {
            mCamera.stopPreview();
        } catch (Exception e) {
            // ignore: tried to stop a non-existent preview
        }

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

        } catch (Exception e) {
            Log.d(TAG, "Error starting camera preview: " + e.getMessage());
        }
    }

}

方法

   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)

3 个答案:

答案 0 :(得分:1)

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

  try {
    mMediaRecorder.start();
    } catch (Exception e) {

答案 1 :(得分:0)

实际上对我来说,为摄像机配置文件设置的尺寸和设置为Media Recorder的尺寸不匹配。

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);

谢谢。

答案 2 :(得分:0)

就我而言:

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

 //step 2:
 mediarecorder.stop();  

如果在步骤1之前调用stop(),则会发生此错误:

MediaRecorder.stop()停止失败:-1007