angel_30
angel_30

Reputation: 1

Change camera recording codec programmatically in Android

I'm using the following code to record the camera in Android. How can I change the default codec (the default is H264) here?

private Uri fileUri;
//...
        private void recordVideo() {
            Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
            fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO);
            // set video quality
            intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file
    
            // start the video capture Intent
            startActivityForResult(intent, CAMERA_CAPTURE_VIDEO_REQUEST_CODE);
        }
    
    // ...
    
    @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE && resultCode == RESULT_OK) {
                // play the video given the global fileUri 
    }

Upvotes: 1

Views: 726

Answers (1)

Alex Cohn
Alex Cohn

Reputation: 57203

You cannot achieve this with ACTION_VIDEO_CAPTURE intent. You have to open the camera in your app and handle recording video by yourself. The official example using CameraX library are a good starting point.

To control the codec, you need an extra step. You can borrow from this answer, which enforces h264 – but you are free to choose MediaCodec.createEncoderByType() the way you like.

Upvotes: 1

Related Questions