Android Camera 2 Api

时间:2015-01-12 17:00:47

标签: android android-camera

我一直在尝试使用camera2 API。我已经从

下载了代码

https://developer.android.com/samples/Camera2Video/index.html了解它是如何运作的。它工作正常,直到我停止录制。当我停止录制时,它会按照代码运行。

 private void stopRecordingVideo() {
        // UI
        mIsRecordingVideo = false;
        mBtn_Video.setText(R.string.record);
        // Stop recording
        try {
            mMediaRecorder.stop();
            mMediaRecorder.reset();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        Activity activity = getActivity();
        if (null != activity) {
            System.out.println("file " +  getVideoFile(activity));
            Toast.makeText(activity, "Video saved: " + getVideoFile(activity),
                    Toast.LENGTH_SHORT).show();
        }
        startPreview();

在mMediaRecorder.stop();它抛出以下错误

01-12 16:24:23.115    2161-2200/com.cameratwoapi E/Surface﹕ queueBuffer: error queuing buffer to SurfaceTexture, -19
01-12 16:24:23.135    2161-2200/com.cameratwoapi E/EGL_emulation﹕ tid 2200: swapBuffers(285): error 0x3003 (EGL_BAD_ALLOC)
01-12 16:24:23.197    2161-2200/com.cameratwoapi E/CameraDeviceGLThread-0﹕ Received exception on GL render thread:
    java.lang.IllegalStateException: swapBuffers: EGL error: 0x3003
            at android.hardware.camera2.legacy.SurfaceTextureRenderer.checkEglError(SurfaceTextureRenderer.java:487)
            at android.hardware.camera2.legacy.SurfaceTextureRenderer.swapBuffers(SurfaceTextureRenderer.java:480)
            at android.hardware.camera2.legacy.SurfaceTextureRenderer.drawIntoSurfaces(SurfaceTextureRenderer.java:681)
            at android.hardware.camera2.legacy.GLThreadManager$1.handleMessage(GLThreadManager.java:103)
            at android.os.Handler.dispatchMessage(Handler.java:98)
            at android.os.Looper.loop(Looper.java:135)
            at android.os.HandlerThread.run(HandlerThread.java:61)

任何想法我做错了什么。我花了几个小时但找不到任何解决方案。

编辑 - 我正在使用geneymotion模拟器。我正在使用的路径

file /storage/emulated/0/Android/data/com.gold.cameratwoapi/files/video.mp4

由于

5 个答案:

答案 0 :(得分:26)

我的解决方案是将void stopRecordingVideo()更改为:

private void stopRecordingVideo() {
// UI
mIsRecordingVideo = false;
mButtonVideo.setText(R.string.record);
// Added by Ben Ning, to resolve exception issue when stop recording.
try {
    mPreviewSession.stopRepeating();
    mPreviewSession.abortCaptures();
} catch (CameraAccessException e) {
    e.printStackTrace();
} 

// Stop recording
mMediaRecorder.stop();
mMediaRecorder.reset();

}

关键是:

    try {
    mPreviewSession.stopRepeating();
    mPreviewSession.abortCaptures();
} catch (CameraAccessException e) {
    e.printStackTrace();
} 

答案 1 :(得分:5)

        private void stopRecordingVideo() {
// UI
        mIsRecordingVideo = false;
        mButtonVideo.setText(R.string.record);
// Added by Ben Ning, to resolve exception issue when stop recording.
        try {
            mPreviewSession.stopRepeating();
            mPreviewSession.abortCaptures();
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }

// Stop recording
        mMediaRecorder.stop();
        mMediaRecorder.reset();

        Activity activity = getActivity();
        if (null != activity) {
            Toast.makeText(activity, "Video saved: " + getVideoFile(activity),
                    Toast.LENGTH_SHORT).show();
        }
        startPreview();
    }

这对我有用。

答案 2 :(得分:3)

调用mMediaRecorder.stop()后,始终会抛出IllegalStateException。我注意到,在INFO_SUPPORTED_HARDWARE_LEVEL_LEGACY CameraDevice将设置更改为错误的设备上,立即在onError()中调用CameraDevice.StateCallback

在您引用的示例中,onError()关闭相机并完成活动,因此只需更改onError()即可重新打开相机,如下所示:

@Override
public void onError(CameraDevice cameraDevice, int error) {
    // mCameraOpenCloseLock.release();
    // cameraDevice.close();
    // mCameraDevice = null;
    // Activity activity = getActivity();
    // if (null != activity) {
    //  activity.finish();
    // }

    closeCamera();
    openCamera(mTextureView.getWidth(), mTextureView.getHeight());
}

在那里进行一些检查也是一个好主意,以确保如果确实发生了错误,则会调用注释掉的代码,而不是进入尝试打开相机的循环,结束了。

使用Android 5.0.2在Moto G 2nd gen上测试

答案 3 :(得分:0)

这取决于您使用CameraCaptureSession和MediaRecorder进行的操作,但是当您调用->select('products.product_name', 'skus.quantity') ->where('products.products_id', '1') ->join('skus', 'skus.products_id', '=', 'products.products_id') ->find('1'); 时,我认为它正在破坏用于相机预览会话的表面,这会导致此错误,因为文档说明

  

一旦录制停止,您将不得不再次对其进行配置,就像它刚刚构建一样

因此,如果您拨打mMediaRecorder.stop()(我收集的PreviewSession.abortCaptures()并不是必需的话),它会停止相机将输出发送到录像机表面,这样您就可以毫无问题地停止MediaRecorder。

mPreviewSession.stopRepeating();没有立即停止相机预览输出,因此您可能会发现需要使用PreviewSession.abortCaptures();MediaRecorder.stop() onClosed()方法调用onReady() 3}}

答案 4 :(得分:0)

在我的情况下,我使用TimerTaskHandler。有一个错误直接到mMediaRecorder.stop()。所以我使用这种方法

final Handler mTimerHandler = new Handler(Looper.getMainLooper());

        mIsRecordingVideo = false;
        // Stop recording
        try {
            mPreviewSession.stopRepeating();
            mPreviewSession.abortCaptures();
        } catch (CameraAccessException e) {
            e.printStackTrace();
        }
        try{
            Timer timer = new Timer();
            TimerTask timerTask = new TimerTask() {
                @Override
                public void run() {
                    mTimerHandler.post(new Runnable() {
                        @Override
                        public void run() {

                            mMediaRecorder.stop();
                            mMediaRecorder.reset();
                        }

                    });
                }
            };
            timer.schedule(timerTask,30);
        }catch(RuntimeException e){
            Log.e("----------------","---->>>>>>>>>"+e);
            e.printStackTrace();
        }