Android record video

In this Android tip, I am going to show you how to start Camera application to record video and display the video in the VideoView widget.
To start the Camera application of an Android device to record video, you need to construct an Intent object with passing the MediaStore.ACTION_VIDEO_CAPTURE value to its constructor. Then call the startActivityForResult() method to start the Camera application. The Camera application returns the video to onActivityResult() as a Uri pointing to the video location in storage. You can use the getData() method of the Intent to get the Uri. Then set the Uri to the VideoView by using the setVideoURI() method. You will use the setMediaController() method to set a media control to the VideoView so that you can play, pause, and move the video forward and backward. The start() of the VideoView must be called to show the video.


Now to have an example application on recording video in Android, you create a new Android project. Then modify the activity_main.xml file to add one VideoView and one Button. The VideoView displays the video returned from the Camera application and the Button is clicked to start the Camera application.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  tools:context=".MainActivity"

  >
  
  <VideoView
    android:id="@+id/videoview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_above="@+id/bt1"
     />
  <Button
   android:id="@+id/bt1"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:text="Start Camera"
   android:onClick="startCamera"
   android:layout_alignParentBottom="true"
   android:layout_centerHorizontal="true"
  
   />
  
</RelativeLayout>


In the MainActivity class, you need to add two methods: startCamera() and onActivityResult(). Then startCamera() method is invoked to start the Camera application for video recording when you click the Start Camera button. In the onActivityResult() method, you receive the returned video and show it in the VideoView.
package com.example.androidexample;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.MediaController;
import android.widget.VideoView;

public class MainActivity extends Activity {
private static final int CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE = 10;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

public void startCamera(View view)
{
//create Intent to record video and return it to the calling application
Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
//start the video capture Intent
startActivityForResult(intent, CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE);

}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_VIDEO_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
//display the video in VideoView
VideoView videoView=(VideoView)findViewById(R.id.videoview);
//get video uri
   Uri videoUri=data.getData();
   //set the video uri to VideoView
   videoView.setVideoURI(videoUri);
   //set media controller allowing you to play, pause, and move the video forward and backward
   videoView.setMediaController(new MediaController(this));
   //play the video
   videoView.start();

} else if (resultCode == RESULT_CANCELED) {
// User cancelled the video capture
} else {
// video capture failed
}
}
}

public boolean onCreateOptionsMenu(Menu menu) {
//Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

}

Android-record-video

Posted by: Dara | post date: 09-18-2014 | Subject: Android Apps Development

write programming tip



This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.