6
ACADGILD ACADGILD 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: 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. https://acadgild.com/blog/wp-admin/post.php?post=11252&action=edit https://acadgild.com/blog/wp-admin/post.php?post=11252&action=edit

Android video capture example using camera

Embed Size (px)

Citation preview

Page 1: Android video capture example using camera

ACADGILDACADGILD

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:

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.

https://acadgild.com/blog/wp-admin/post.php?post=11252&action=edithttps://acadgild.com/blog/wp-admin/post.php?post=11252&action=edit

Page 2: Android video capture example using camera

ACADGILDACADGILD

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>

https://acadgild.com/blog/wp-admin/post.php?post=11252&action=edithttps://acadgild.com/blog/wp-admin/post.php?post=11252&action=edit

Page 3: Android video capture example using camera

ACADGILDACADGILD

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 meansit 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) {

https://acadgild.com/blog/wp-admin/post.php?post=11252&action=edithttps://acadgild.com/blog/wp-admin/post.php?post=11252&action=edit

Page 4: Android video capture example using camera

ACADGILDACADGILD

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);}

Output:

Before Capture Video

After Capture Video

https://acadgild.com/blog/wp-admin/post.php?post=11252&action=edithttps://acadgild.com/blog/wp-admin/post.php?post=11252&action=edit

Page 5: Android video capture example using camera

ACADGILDACADGILD

While Play Video

Conclusion:

This is the basic idea of how to create a similar camera application. We can customize it as per the need of the application and make it even better. It only shows how to launch the camera activity and get the data in return to capture the video. It finds usage in popular applications like Instagram. The camera functionality plays an important role in many applications and it is very popular in Android development.

https://acadgild.com/blog/wp-admin/post.php?post=11252&action=edithttps://acadgild.com/blog/wp-admin/post.php?post=11252&action=edit

Page 6: Android video capture example using camera

ACADGILDACADGILD

https://acadgild.com/blog/wp-admin/post.php?post=11252&action=edithttps://acadgild.com/blog/wp-admin/post.php?post=11252&action=edit