When user click on the "Playback" button, the video will be played on the VideoView if exist.
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
No comments:
Post a Comment