Sunday, March 10, 2013

Android video capturing and playing

In android we cam make a video recorder and player, we just need to follow few steps as follows.
1- make a project with the name VideoComponent and have a main activity VideoComponent.java

package com.jitesh.video;

import com.jitesh.video.R;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

/*To allow recording video, add permission into the application's manifest file. i.e. 
Add <uses-permission android:name="android.permission.RECORD_VIDEO"/>*/

//class which gives choice to start video player or  video recorder
public class VideoComponent extends Activity 
{
private Button playerbtn;//button to start player
private Button recorderbtn;//button for recorder
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
playerbtn= (Button)findViewById(R.id.player);
recorderbtn = (Button)findViewById(R.id.recorder);
//start the VideoPlayer on the click of the button 
   playerbtn.setOnClickListener(new View.OnClickListener(){
  public void onClick(View view){
  Intent intent = new Intent(VideoComponent.this, VideoPlayer.class);
          startActivity(intent);
       
   }
       });
   
 //start the VideoRecorder on the click of the button 
  recorderbtn.setOnClickListener(new View.OnClickListener(){
  public void onClick(View view){
  Intent intent = new Intent(VideoComponent.this, VideoRecorder.class);
          startActivity(intent);
   }
      });
   }

}
2-VideoRecorder.java

package com.jitesh.video;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;

public class VideoRecorder extends Activity{
//Create objects of MediaRecorder and Preview class  
    private MediaRecorder recorder;
private Preview mPreview;
boolean flag=false; 
boolean startedRecording=false;
boolean stoppedRecording=false;
// In this method, create an object of MediaRecorder class. Create an object of 
    // RecorderPreview class(Customized View). Add RecorderPreview class object
    // as content of UI.     
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
recorder = new MediaRecorder();
  recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
  recorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
  recorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
  mPreview = new Preview(VideoRecorder.this,recorder);
  setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
  setContentView(mPreview);
 
   }
/*!
<p>
    Initialize the contents of the Activity's standard options menu. Menu items are to be placed in to menu.
    This is called on each press of menu button. In this options to start and stop recording are provided. 
    Option for start recording  has group id 0 and option to stop recording is 1.
    (first parameter of menu.add method). Start and stop have different group id, if recording is already 
    started then it shows stop option else it shows start option.
</p>*/   
@Override
public boolean onPrepareOptionsMenu(Menu menu) 
{
super.onPrepareOptionsMenu(menu);
menu.clear(); 
menu.add(0, 0, 0, "Start Recording"); 
menu.add(1, 1, 0, "Stop Recording");
menu.setGroupVisible(0, false);
menu.setGroupVisible(1, false);
if(startedRecording==false)
menu.setGroupVisible(0, true);
else if(startedRecording==true&&stoppedRecording==false)
menu.setGroupVisible(1, true);
return true;
}

/*!
<p>
   This method receives control when Item in menu option is selected. It contains implementations
   to be performed on selection of menu item. 
   </p>*/
   
@Override
public boolean onOptionsItemSelected(MenuItem item) 
{
switch (item.getItemId()) 
{
case 0:
//start the recorder
recorder.start();
startedRecording=true;
break;

case 1: 
//stop the recorder
recorder.stop();
recorder.release();
recorder = null;
stoppedRecording=true;
break;
default:
break;
}
return super.onOptionsItemSelected(item);
}
class Preview extends SurfaceView implements SurfaceHolder.Callback
{
//Create objects for MediaRecorder and SurfaceHolder.
SurfaceHolder mHolder;
MediaRecorder tempRecorder;

//Create constructor of Preview Class. In this, get an object of 
   //surfaceHolder class by calling getHolder() method. After that add   
   //callback to the surfaceHolder. The callback will inform when surface is 
   //created/changed/destroyed. Also set surface not to have its own buffers.
public Preview(Context context,MediaRecorder recorder) {
super(context);
tempRecorder=recorder;
mHolder=getHolder();
mHolder.addCallback(this);
mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
// TODO Auto-generated constructor stub
}

public Surface getSurface()
{
return mHolder.getSurface();
}
// Implement the methods of SurfaceHolder.Callback interface

   // SurfaceCreated : This method gets called when surface is created.
   // In this, initialize all parameters of MediaRecorder object.
//The output file will be stored in SD Card.
public void surfaceCreated(SurfaceHolder holder){
tempRecorder.setOutputFile("/mnt/sdcard/recordvideooutput.3gpp");
tempRecorder.setPreviewDisplay(mHolder.getSurface());
try{
tempRecorder.prepare();
} catch (Exception e) {
String message = e.getMessage();
tempRecorder.release();
tempRecorder = null;
}
}

public void surfaceDestroyed(SurfaceHolder holder) 
{
if(tempRecorder!=null)
{
tempRecorder.release();
tempRecorder = null;
}
}

public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) 
{

}
}   
}

 3-VideoPlayer.java

package com.jitesh.video;

import com.jitesh.video.R;

import android.app.Activity;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.Window;
import android.widget.MediaController;
import android.widget.VideoView;

public class VideoPlayer extends Activity{
VideoView videoView;
public void onCreate(Bundle savedInstanceState) 
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//Create a VideoView widget in the layout file
//use setContentView method to set content of the activity to the layout file which contains videoView
this.setContentView(R.layout.videoplayer);
        videoView = (VideoView)this.findViewById(R.id.videoView);
        
        //add controls to a MediaPlayer like play, pause.
        MediaController mc = new MediaController(this);
        videoView.setMediaController(mc);
        
        //Set the path of Video or URI
        videoView.setVideoPath("/mnt/sdcard/recordvideooutput.3gpp");
        
      //Set the focus
        videoView.requestFocus();
   }
}

4) main1.xml

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

    <Button
        android:id="@+id/recorder"
        android:layout_width="match_parent"
        android:layout_height="50px"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20px"
        android:text="Recorder"
        android:textColor="#330000"
        android:textSize="15px"
        android:typeface="serif" />

    <Button
        android:id="@+id/player"
        android:layout_width="match_parent"
        android:layout_height="50px"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20px"
        android:text="Player"
        android:textColor="#330000"
        android:textSize="15px"
        android:typeface="serif" />

</LinearLayout>

5) videoplayer.xml

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

    <VideoView
        android:id="@+id/videoView"
        android:layout_width="480px"
        android:layout_height="480px"
        android:layout_x="10px"
        android:layout_y="10px" />

</LinearLayout>

add the permissions as follows at mainfest

 <uses-permission android:name="android.permission.RECORD_VIDEO" />
     <uses-permission android:name="android.permission.CAMERA"/>
      <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>











Download code from the link VIDEOrECORDING EXAMPLE


1 comment:

  1. it was really so nice and exited to have such a experience.
    http://top7mobiles.com/top-list/best-app-to-download-free-movies-on-android

    ReplyDelete