0

I am getting error on capturing video

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.media.MediaCodec.reset()' on a null object reference

Even though i have given Record_Audio permission. I am using camerax version: 1.0.2

// Permission check
if (ActivityCompat.checkSelfPermission(
        requireContext(),
        Manifest.permission.RECORD_AUDIO
    ) != PackageManager.PERMISSION_GRANTED
) {

    return
}

// Video Capture callback
videoCapture.startRecording(getNewVideoOutputFileOptions(requireContext().contentResolver),
    ContextCompat.getMainExecutor(requireContext()),
    object : VideoCapture.OnVideoSavedCallback {
        override fun onVideoSaved(outputFileResults: VideoCapture.OutputFileResults) {
            try {
                mediaUri = outputFileResults.savedUri
              
                val msg = "Video record succeeded: $mediaUri"
                Log.d(TAG, msg)

                   }catch (ex: java.lang.Exception) {
                ex.printStackTrace()
                Toast.makeText(
                    requireContext(),
                    "Error in capturing video!",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }

        override fun onError(videoCaptureError: Int, message: String, cause: Throwable?) {
            Log.e("VIDEO_CAPTURE", "Video capture failed");
            if (cause != null) {
                cause.printStackTrace();
                Toast.makeText(
                    requireContext(),
                    "Error in capturing video!",
                    Toast.LENGTH_SHORT
                ).show()
            }
        }
    }
)
Richa
  • 3,165
  • 1
  • 22
  • 26
  • Please, show your code. In https://stackoverflow.com/questions/42594958/attempt-to-invoke-virtual-method-void-android-media-mediarecorder-prepare-on they forgot to create an object. How do you capture video? – CoolMind Dec 07 '21 at 10:26
  • @CoolMind : take a look – Richa Dec 07 '21 at 10:33
  • Thanks! As far as I understood, stacktrace doesn't contain your code, only SDK exception? – CoolMind Dec 07 '21 at 11:31
  • You can try to create `MediaCodec` object yourself (see https://developer.android.com/reference/kotlin/android/media/MediaCodec) and try to call `reset()` to test if it works. Maybe it depends on device or Android version or codec list. – CoolMind Dec 07 '21 at 11:45
  • See also https://stackoverflow.com/questions/52835987/why-does-mediacodec-reset-generate-java-lang-illegalstateexception. – CoolMind Dec 07 '21 at 11:46
  • given stackoverflow link is not working, still crashing – Richa Dec 07 '21 at 16:23
  • How are you handling the case when recording is in progress and user rotates the device? I'm getting ERROR_SOURCE_INACTIVE in Finalise – Astha Garg Sep 16 '22 at 05:32

1 Answers1

-1

For those who are struggling with MediaCodec exception while recording video. Have a look at CameraX video. For me it solved issue. Hope it will be helpful to someone.

Richa
  • 3,165
  • 1
  • 22
  • 26