android video app adding record timer

The android video app adding record timer tutorial describes how add a timer when recording.

This android tutorial shows how to use the android Chronograph view. Which is a simple view to display the elapsed time.

The timer will only be displayed when the android video app is recording.

Get Code

The code to start this tutorial is on github here

https://github.com/mobapptuts/android_camera2_api_video_app.git Tag camera2-video-chronometer

or you can run this command

git clone https://github.com/mobapptuts/android_camera2_api_video_app.git –branch camera2-video-chronometer

Steps

Add the Chronometer view to the layout

<Chronometer
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/chronometer"
        android:visibility="invisible"
        android:textColor="#ff0000"
        android:textSize="25dp"
        android:layout_above="@+id/videoOnlineImageButton"
        android:layout_alignStart="@+id/videoOnlineImageButton"  />

 Add a Chronometer member to the activity

private Chronometer mChronometer;

 Initialise the chronometer with the chronometer from the layout & add to code to stop the timer

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

        createVideoFolder();


        mMediaRecorder = new MediaRecorder();

        mChronometer = (Chronometer) findViewById(R.id.chronometer);
        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) {
                    mChronometer.stop();
                    mChronometer.setVisibility(View.INVISIBLE);
                    mIsRecording = false;
                    mRecordImageButton.setImageResource(R.mipmap.btn_video_online);
                    mMediaRecorder.stop();
                    mMediaRecorder.reset();
                    startPreview();
                } else {
                    checkWriteStoragePermission();
                }
            }
        });
    }

Add the code to turn on the timer in the checkWriteStoragePermission method

  private void checkWriteStoragePermission() {
        if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if(ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)
                    == PackageManager.PERMISSION_GRANTED) {
                mIsRecording = true;
                mRecordImageButton.setImageResource(R.mipmap.btn_video_busy);
                try {
                    createVideoFileName();
                } catch (IOException e) {
                    e.printStackTrace();
                }
                startRecord();
                mMediaRecorder.start();
                mChronometer.setBase(SystemClock.elapsedRealtime());
                mChronometer.setVisibility(View.VISIBLE);
                mChronometer.start();
            } else {
                if(shouldShowRequestPermissionRationale(Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
                    Toast.makeText(this, "app needs to be able to save videos", Toast.LENGTH_SHORT).show();
                }
                requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_WRITE_EXTERNAL_STORAGE_PERMISSION_RESULT);
            }
        } else {
            mIsRecording = true;
            mRecordImageButton.setImageResource(R.mipmap.btn_video_busy);
            try {
                createVideoFileName();
            } catch (IOException e) {
                e.printStackTrace();
            }
            startRecord();
            mMediaRecorder.start();
            mChronometer.setBase(SystemClock.elapsedRealtime());
            mChronometer.setVisibility(View.VISIBLE);
            mChronometer.start();
        }
    }

For android devices on marshmallow or later this code will need to be added.

Its only used when runtime permissions are first called on application installation first run

private CameraDevice.StateCallback mCameraDeviceStateCallback = new CameraDevice.StateCallback() {
       @Override
       public void onOpened(CameraDevice camera) {
           mCameraDevice = camera;
           if(mIsRecording) {
               try {
                   createVideoFileName();
               } catch (IOException e) {
                   e.printStackTrace();
               }
               startRecord();
               mMediaRecorder.start();
               mChronometer.setBase(SystemClock.elapsedRealtime());
               mChronometer.setVisibility(View.VISIBLE);
               mChronometer.start();
           } else {
               startPreview();
           }
           // Toast.makeText(getApplicationContext(),
           //         "Camera connection made!", Toast.LENGTH_SHORT).show();
       }

 

Android video app adding record timer summary

In the android video app adding record timer tutorial we saw that it was straight forward and easy to add a chronometer view to your layout.

And the android video application provides a good example on using the chronometer view.

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>