Search code examples
androidandroid-studioandroid-timer

Time Difference Check- Android video capture


I am working on a video recording camera APP. App crashes if camera is stopped right after starting it maybe because of video size very less. I want to activate stop button only if video size is greater than 1 sec. But problem is I cannot find Current Time and Start time correctly. Finding the difference of two time factors will help in implementing 2 sec Check. Need Help please.

private void onClickActions(View v)
{
    float tt = start_time /10000000000000f;
    float ct = ((System.currentTimeMillis() ) /10000000000000f);

    Log.d("Before stoping S-Time ",tt+"");
    Log.d("Before stoping C-Time ",ct+"");

    if (recording  && tt>=2.0f)
     {
         Log.d("After Stopping = ",tt+"");
        // stop recording and release camera
        mediaRecorder.stop(); // stop the recording
        recording = false;
        rec.setVisibility(View.INVISIBLE);
        start_time = 0;

    }

    //remove time code to initial revert
    if(v.getId()== start.getId() && ((CameraPreview.recordHappy || CameraPreview.recordSad))) {
        prepareMediaRecorder();
        recording = true;
        mediaRecorder.start();
        consent = true;
        happyRecorded=true;
        stop.setClickable(true);
        start.setClickable(false);

        if (AndroidVideoCaptureExample.iV.getVisibility()==View.VISIBLE)
        AndroidVideoCaptureExample.iV.setVisibility(View.INVISIBLE);
        //AndroidVideoCaptureExample.capture.setText("RECORDING STARTED!");

        rec.setVisibility(View.VISIBLE);
        start_time = (int)(System.currentTimeMillis());
        //Toast.makeText(myContext, "You are being recorded now!", Toast.LENGTH_LONG);

    }
    if(v.getId()== stop.getId() && consent==true && recording==false) {

        if((!CameraPreview.recordHappy && CameraPreview.recordSad))
        {
            releaseMediaRecorder(); // release the MediaRecorder object
            Intent intent = new Intent();
            intent.setClass(AndroidVideoCaptureExample.this, consentActivity.class);
            startActivity(intent);
            finish();
        }
        else {
            CameraPreview.recordHappy = false;
            CameraPreview.recordSad = true;
            stop.setClickable(false);
            start.setClickable(true);
            recording = false;
            AndroidVideoCaptureExample.capture.setText("Record Neutral Moment");
            rec.setVisibility(View.INVISIBLE);

        }
    }
}

Solution

  • I think you might be overengineering a simple thing. You don't really need to count record time unless you are showing it on the UI. If you want to disable the button, simply disable it just before starting the recording, then use Handler to re-enable after 2 seconds:

            new Handler(Looper.getMainLooper()).postDelayed(new Runnable() {
                @Override
                public void run() {
                    // enable stop button
                }
            },2000);
    

    However, I would argue that's not a very good user experience. If you look at cameras like Google Camera, you can stop it immediately after starting, it just won't record anything. To achieve this, you need to catch the RuntimeException when calling mediaRecorder.stop(), then check and clean up the generated file. If it's empty then delete it and don't throw an error to the UI.