Sunday, April 3, 2011

Start Video Recording using android.provider.MediaStore.ACTION_VIDEO_CAPTURE

In this exercise, when user click on the "Recording Video using Intent" button, it will start activity with android.provider.MediaStore.ACTION_VIDEO_CAPTURE to ask Android system to start a app helping record video.
When user click on the "Playback" button, the video will be played on the VideoView if exist.

Start Video Recording using ACTION_VIDEO_CAPTURE

To ask video recording using android.provider.MediaStore.ACTION_VIDEO_CAPTURE, simple call startActivityForResult() method with Intent of android.provider.MediaStore.ACTION_VIDEO_CAPTURE.

In onActivityResult() call-back function, check resultCode and requestCode. If both valid and match, the uri of the recorded video file can be retrieved using data.getData().

package com.exercise.AndroidIntentVideoRecording;

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

public class AndroidIntentVideoRecording extends Activity {

final static int REQUEST_VIDEO_CAPTURED = 1;
Uri uriVideo = null;
VideoView videoviewPlay;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonRecording = (Button)findViewById(R.id.recording);
Button buttonPlayback = (Button)findViewById(R.id.playback);
videoviewPlay = (VideoView)findViewById(R.id.videoview);

buttonRecording.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(android.provider.MediaStore.ACTION_VIDEO_CAPTURE);
startActivityForResult(intent, REQUEST_VIDEO_CAPTURED);
}});

buttonPlayback.setOnClickListener(new Button.OnClickListener(){

@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(uriVideo == null){
Toast.makeText(AndroidIntentVideoRecording.this,
"No Video",
Toast.LENGTH_LONG)
.show();
}else{
Toast.makeText(AndroidIntentVideoRecording.this,
"Playback: " + uriVideo.getPath(),
Toast.LENGTH_LONG)
.show();
videoviewPlay.setVideoURI(uriVideo);
videoviewPlay.start();
}
}});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
if(resultCode == RESULT_OK){
if(requestCode == REQUEST_VIDEO_CAPTURED){
uriVideo = data.getData();
Toast.makeText(AndroidIntentVideoRecording.this,
uriVideo.getPath(),
Toast.LENGTH_LONG)
.show();
}
}else if(resultCode == RESULT_CANCELED){
uriVideo = null;
Toast.makeText(AndroidIntentVideoRecording.this,
"Cancelled!",
Toast.LENGTH_LONG)
.show();
}
}
}


main.xml
<?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"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button
android:id="@+id/recording"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Recording Video using Intent"
/>
<Button
android:id="@+id/playback"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Playback"
/>
<VideoView
android:id="@+id/videoview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>


Download the files.

Related article:
- CamcorderProfile: predefined camcorder profile settings for camcorder applications
- A simple exercise of Video Capture using MediaRecorder
- Using MediaStore.EXTRA_OUTPUT specify target file for MediaStore.ACTION_VIDEO_CAPTURE

9 comments:

fireboypr said...

Hi, Your code helped me a lot understanding the video record process. Can you let me know if or how I can change the file name and save path of the video? thanks in advance

Erik said...

hello ZeRo-cOoL,

I think you can implement your own activity to capture video. Pls refer A simple exercise of Video Capture using MediaRecorder.

fireboypr said...

Hi, thanks for the reference. But I managed to worked with the built in camera application to take picture and save them in a custom path with a custom file name. I would like to do the same with the built in video recorder application. Is it possible? I found how to get the real path from the URI but haven't been able to change it, or the file name either. thanks for your quick reply

Erik said...

hello ZeRo-cOoL,

Please read Using MediaStore.EXTRA_OUTPUT specify target file for MediaStore.ACTION_VIDEO_CAPTURE.

SKC said...

Great app, but can you tell me how can i capture the screen and then save and display that as a video file.

SKC said...

Can you please tell me how do i do a screen capture and then display them as a video file.

Erik said...

Sorry SKC,

I have no idea right now!

Anonymous said...

Hi , in my project i need to capture
the screen and display the video file can you help me

Appledeveloper said...

the intent is not recording video automatically. for recording video automatically what to do?