Free Shipping

Secure Payment

easy returns

24/7 support

  • Home
  • Blog
  • Android Video Capture Example using Camera

Android Video Capture Example using Camera

 July 8  | 0 Comments

Introduction:

If we want to use the inbuilt Camera of our device by your application then we have to initiate the Intent for it.
By using android.provider.MediaStore class we can easily capture the video.
The device Camera application requests video capture using android.provider.MediaStore.ACTION_VIDEO_CAPTURE.
To launch the camera there is a need to initiate the intent by using startActivity(..) or startActivityForResult(..) method.

startActivity() and startActivityForResult() with Camera Video Capturing:

startActivity():

If we want to launch the Camera application without worrying about the result of the captured video,
then we can call this method:

Get Skilled in Android Development
Intent captureVideoIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivity(captureVideoIntent);

startActivityForResult():

Here, to access the captured video from a camera activity, the camera intent must be initiated using this call. It not only allows to start the camera application, but also one can also expect to get a result in form of a captured video.

Intent captureVideoIntent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(captureVideoIntent,VIDEO_CAPTURED);

To access the captured video we will override the callback method onActivityResult(…).
Here we get the data and compare the request code, which is the second argument of the startActivityForResult() method, that the video has captured.

Example with Code:

Let’s see how we can create the Camera video capturing example by using startActivity() and startActivityForResult() method.

STEPS:

  1. Create new Application CameraVideoCapturingExample.

AndroidManifest.xml:

First, we have to add some permissions to create the Camera application.
Add this in your manifest file.

<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />

activity_main.xml:

Inside this, we add the three buttons and a video view.

<Button android:text="Capture Video"
android:id="@+id/CaptureVideoButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button android:text="Capture Video without Data"
android:id="@+id/CaptureVideoWithoutDataButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button android:text="Play Video"
android:id="@+id/PlayVideoButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<VideoView android:id="@+id/VideoView"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</VideoView>

1st button is for capturing the video by expecting a result, which means it uses startActivityForResult().
2nd button is for only capturing the video without accessing the video, which means it uses startActivity().
3rd is to play the captured video. There is a condition that, if we capture the video by using startActivityForResult() then the play video button is enabled otherwise not.
VideoView is used to play the video that has been captured.

MainActivity.java:

Here we will be using the VideoView object with Uri to play the captured video.

VideoView videoView;
UrivideoFileUri;
public static int VIDEO_CAPTURED = 1;

It is a constant which is used as a request code in onActivityResult() method.

Add some code in onCreate(){…}

Take the reference of all buttons and video views and set the listeners on buttons.

captureVideoButton = (Button) this.findViewById(R.id.CaptureVideoButton);
playVideoButton = (Button) this.findViewById(R.id.PlayVideoButton);
captureWithoutDataVideoButton = (Button) this.findViewById(R.id.CaptureVideoWithoutDataButton);
videoView = (VideoView) this.findViewById(R.id.VideoView);
captureVideoButton.setOnClickListener(this);
playVideoButton.setOnClickListener(this);
captureWithoutDataVideoButton.setOnClickListener(this);

Initially, playVideoButton is not enabled. We will set enable when we capture the video.

playVideoButton.setEnabled(false);

Inside onClick() we can do this like:

if (v == captureVideoButton) {
Intent captureVideoIntent =new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(captureVideoIntent,VIDEO_CAPTURED);
}if (v == captureWithoutDataVideoButton) {
playVideoButton.setEnabled(false);
Intent captureVideoIntent =new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivity(captureVideoIntent);
}else if (v == playVideoButton) {
videoView.setVideoURI(videoFileUri);
videoView.start();
}

And our captured video will be accessed through onActivityResult() method

Add some code in onActivityResult() method:

if (resultCode == RESULT_OK && requestCode == VIDEO_CAPTURED) {
videoFileUri = data.getData();
playVideoButton.setEnabled(true);
>