android studio debugging workaround fix

Introduction

The android studio debugging workaround fix tutorial episode describes how to implement a fix for for Samsung 5 series devices which are having record problems.

The device I have used to replicate this issue is the Samsung Note 4 device.

Diagnosis

From the previous tutorial we believed we have found the cause of the problem. Which is the record capture session onClose method is not being called. Resulting in the camera2 surface outputs not being released prior to the preview session being created. This is causing a illegalStateException and the android camera2 video application to freeze and crash.

Solution

The aim for addressing the suspected problem is to ensure the record capture session is completed and closed prior to starting the preview capture session.

The steps to achieving this are:

  1. Manually calling close to the record capture session when the recording is completed
  2. Once the record capture session onClose() method is called, call the startPreview method

Code

Create a member for the record capture session

    private CameraCaptureSession mRecordCaptureSession;

Implement the member in the startRecord method

private void startRecord() {

       try {
           setupMediaRecorder();
           SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
           surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
           Surface previewSurface = new Surface(surfaceTexture);
           Surface recordSurface = mMediaRecorder.getSurface();
           mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
           mCaptureRequestBuilder.addTarget(previewSurface);
           mCaptureRequestBuilder.addTarget(recordSurface);

           mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, recordSurface),
                   new CameraCaptureSession.StateCallback() {
                       @Override
                       public void onConfigured(CameraCaptureSession session) {
                           Log.d(TAG, "RS");
                           mRecordCaptureSession = session;
                           try {
                               mRecordCaptureSession.setRepeatingRequest(
                                       mCaptureRequestBuilder.build(), null, null
                               );
                           } catch (CameraAccessException e) {
                               e.printStackTrace();
                           }
                       }

                       @Override
                       public void onConfigureFailed(CameraCaptureSession session) {

                       }

                       @Override
                       public void onClosed(CameraCaptureSession session) {
                           super.onClosed(session);
                           Log.d(TAG, "RC");
                       }
                   }, null);


       } catch (Exception e) {
           e.printStackTrace();
       }
   }

In the record button when recording has stopped close the record capture session

And also comment out the startPreview call

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_camera2_video_image);

        createVideoFolder();


        mMediaRecorder = new MediaRecorder();

        mTextureView = (TextureView) findViewById(R.id.textureView);
        mRecordImageButton = (ImageButton) findViewById(R.id.videoOnlineImageButton);
        mRecordImageButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mIsRecording) {
                    mRecordCaptureSession.close();
                    mIsRecording = false;
                    mRecordImageButton.setImageResource(R.mipmap.btn_video_online);
                    mMediaRecorder.stop();
                    mMediaRecorder.reset();
                    // startPreview();
                } else {
                    checkWriteStoragePermission();
                }
            }
        });
    }

In the record capture session onClose method call startPreview

private void startRecord() {

        try {
            setupMediaRecorder();
            SurfaceTexture surfaceTexture = mTextureView.getSurfaceTexture();
            surfaceTexture.setDefaultBufferSize(mPreviewSize.getWidth(), mPreviewSize.getHeight());
            Surface previewSurface = new Surface(surfaceTexture);
            Surface recordSurface = mMediaRecorder.getSurface();
            mCaptureRequestBuilder = mCameraDevice.createCaptureRequest(CameraDevice.TEMPLATE_RECORD);
            mCaptureRequestBuilder.addTarget(previewSurface);
            mCaptureRequestBuilder.addTarget(recordSurface);

            mCameraDevice.createCaptureSession(Arrays.asList(previewSurface, recordSurface),
                    new CameraCaptureSession.StateCallback() {
                        @Override
                        public void onConfigured(CameraCaptureSession session) {
                            Log.d(TAG, "RS");
                            mRecordCaptureSession = session;
                            try {
                                mRecordCaptureSession.setRepeatingRequest(
                                        mCaptureRequestBuilder.build(), null, null
                                );
                            } catch (CameraAccessException e) {
                                e.printStackTrace();
                            }
                        }

                        @Override
                        public void onConfigureFailed(CameraCaptureSession session) {

                        }

                        @Override
                        public void onClosed(CameraCaptureSession session) {
                            super.onClosed(session);
                            Log.d(TAG, "RC");
                            startPreview();
                        }
                    }, null);


        } catch (Exception e) {
            e.printStackTrace();
        }
    }

 

Android studio debugging workaround fix summary

In the android studio debugging workaround fix tutorial we learnt how to implement a workaround fix based on the findings from the previous tutorial.

The concludes the android studio debugging series. In where an example of the debugging lifecycle was shown. Debugging is an skill and some may refer to it as an art. But for those new to android application debugging it’s recommended to have a defined workflow in place prior to addressing the issue.

 

 

About The Author
-

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>