Search code examples
javaandroidcameravideo-capture

How can I enable auto start recording video without pressing record button?


I am developing an application where I press record button and it will automatically start video recording and stop after given duration. But what I found is just open video recording , I have to manually click record button and to save video, I have to click OK button.

Is there any way to do that automatically?

Here is my code:

public class MainActivity extends AppCompatActivity {
    private static  int CAMERA_PERMISSION_CODE=100;
    private static int VIDEO_RECORD_CODE=101;
    public Uri videoPath;
    UploadTask uploadTask;
    Button record_btn;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        record_btn=findViewById(R.id.record_button);
        storageReference=FirebaseStorage.getInstance().getReference();

        if (isCameraPresent()){
            Log.i("VIDEO_RECORD_TAG","Camera is Detected");
            getCameraPermission();
        }else{
            Log.i("VIDEO_RECORD_TAG","No Camera is detected");
        }


        record_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                recordVideo();
            }
        });
    }

    private boolean isCameraPresent(){
        if (getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_ANY)){
            return true;
        }else {
            return false;
        }
    }

    private void getCameraPermission(){
        if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA)
                == PackageManager.PERMISSION_DENIED){
            ActivityCompat.requestPermissions(this, new String[]
                    {Manifest.permission.CAMERA},CAMERA_PERMISSION_CODE);
        }
    }

    private void recordVideo(){
        Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
        intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 5);
        startActivityForResult(intent,VIDEO_RECORD_CODE);

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode==VIDEO_RECORD_CODE){

            if (resultCode==RESULT_OK){
                assert data != null;
                videoPath=data.getData();
                Log.i("VIDEO_RECORD_TAG","Video is recorded and available at path" + videoPath);
                
            }
            else {
                Log.i("VIDEO_RECORD_TAG","Video recording has some error");
            }
        }
    }

Solution

  • You're using an Intent to capture video. What that does is literally ask another app to record video for you. When you're doing that you have to use that app as it is, which requires a button press. If you want to avoid that, you're going to have to directly use the camera yourself and handle the recording.

    As an aside- if you're using an Intent to record video, you don't need Camera permission. You do if you're recording it yourself.