Recording Video Using an Intent

It is becoming cliché, but as previously discussed, often the quickest and easiest way to perform some function on Android is to leverage an existing application that can be triggered by an intent from our application. Using the built-in Camera application to record video, triggered by an intent, is no exception.

Within the android.provider.MediaStore class is a constant named ACTION_VIDEO_CAPTURE, which contains the string “android.media.action. VIDEO_CAPTURE”. This string is registered by the Camera application as an intent filter and will therefore be activated by an intent sent via the Content.startActivity or Context.startActivityForResult methods. Other applications may also register the same string, which would result in the user being prompted to choose which application he or she would like to use to perform the action.

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

VIDEO_CAPTURED is a constant that should be defined as a class variable and is used to recognize when the Camera application returns a result to our activity via a call to our onActivityResult method:

public static int VIDEO_CAPTURED = 1;

The intent that is passed back to our activity in the onActivityResult method (data in the following code example) contains a Uri to the video file that was created by the Camera application.

protected void onActivityResult (int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK && requestCode == VIDEO_CAPTURED) {
                Uri videoFileUri = data.getData();
        }
}

Here is a full example that uses an intent to trigger the built-in Camera application for video capture.

package com.apress.proandroidmedia.ch11.videocaptureintent;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.VideoView;

public class VideoCaptureIntent extends Activity implements OnClickListener {

The VIDEO_CAPTURED constant is created to the activity that is returning via a call to onActivityResult.

    public static int VIDEO_CAPTURED = 1;

We'll use two buttons, one for triggering the sending of the intent, captureVideoButton, and another for playing the video once it is captured, playVideoButton.


    Button captureVideoButton;
    Button playVideoButton;

We will be using a standard VideoView object with a Uri to play back the video that has been captured.

    VideoView videoView;
    Uri videoFileUri;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        captureVideoButton = (Button) this.findViewById(R.id.CaptureVideoButton);
        playVideoButton = (Button) this.findViewById(R.id.PlayVideoButton);

        captureVideoButton.setOnClickListener(this);
        playVideoButton.setOnClickListener(this);

The playVideoButton will initially not be enabled, meaning it cannot be clicked. We'll set it to be enabled once we have captured the video.

        playVideoButton.setEnabled(false);

        videoView = (VideoView) this.findViewById(R.id.VideoView);
    }

Our activity implements OnClickListener and is registered as the OnClickListener for each of the Buttons. Therefore when a button is pressed or clicked, our onClick method will be called.

        public void onClick(View v) {
                if (v == captureVideoButton) {

If the captureVideoButton was pressed, we create the intent and pass it along with our VIDEO_CAPTURED constant into the startActivityForResult method, which will kick off the built-in Camera application.

                        Intent captureVideoIntent = new Intent(android.provider.Image
MediaStore.ACTION_VIDEO_CAPTURE);
                        startActivityForResult(captureVideoIntent, VIDEO_CAPTURED);
                } else if (v == playVideoButton) {

If the playVideoButton is pressed, we'll set the Uri to play and start the playback.

                        videoView.setVideoURI(videoFileUri);
                        videoView.start();
                }
        }

When the Camera (or any triggered application/activity) returns, the following onActivityResult method will be called. We check that the resultCode is the constant RESULT_OK and the requestCode is what we passed into the startActivityForResult, VIDEO_CAPTURED, and then grab the Uri to the video file that was recorded. Following that, we'll enable the playVideoButton so the user can press it and trigger the video to play.

        protected void onActivityResult (int requestCode, int resultCode, Intent data) {
                if (resultCode == RESULT_OK && requestCode == VIDEO_CAPTURED) {
                        videoFileUri = data.getData();
                        playVideoButton.setEnabled(true);
                }
        }

}

Here is the layout XML contained in res/layout/main.xml referenced by the foregoing activity.

<?xml version="1.0" encoding="utf-8"? >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >

        <Button android:text="Capture Video" android:id="@+id/CaptureVideoButton"Image
 android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <Button android:text="Play Video" android:id="@+id/PlayVideoButton"Image
 android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
        <VideoView android:id="@+id/VideoView" android:layout_width="wrap_content"Image
 android:layout_height="wrap_content"></VideoView>
</LinearLayout>

As we have discovered, if we simply need to record video or want to offer the user all of the controls available in the Camera app, using an intent to trigger it is a great way to go.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.120.206