Monday, 7 December 2015

Video recording example in android

activity_main.xml:
<RelativeLayout
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <SurfaceView
        android:id="@+id/surfaceView1"
        android:layout_width="fill_parent"
        android:layout_height="480px"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true" />

<Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/surfaceView1"
        android:layout_marginLeft="38dp"
        android:layout_marginTop="44dp"
        android:onClick="start"
        android:text="Start" />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_centerHorizontal="true"
        android:onClick="stop"
        android:text="Stop" />

    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button2"
        android:layout_alignBottom="@+id/button2"
        android:layout_alignParentRight="true"
        android:layout_marginRight="14dp"
        android:onClick="camera"
        android:text="Camera" />

<RelativeLayout/>





Step2:
MainActivity.java:


package com.example.videorecordingex;

import java.io.File;

import android.app.Activity;
import android.content.Intent;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;

public class MainActivity extends Activity {


MediaRecorder recorder;

SurfaceView view; 

SurfaceHolder holder;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

}

public void start(View v){

initialization(); 
recorder.start(); 
}
 

private void initialization() {



view=(SurfaceView)findViewById(R.id.surfaceView1);

holder=view.getHolder();

holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);


recorder=new MediaRecorder();

recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
recorder.setOutputFile(Environment.getExternalStorageDirectory()+"/new_video.mp4");

CamcorderProfile profile=CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);

recorder.setProfile(profile);

recorder.setPreviewDisplay(holder.getSurface()); 


try {
recorder.prepare();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();

}

public void stop(View v){

recorder.stop();   

}

public void camera(View v){

Intent i = new Intent("android.media.action.IMAGE_CAPTURE");
        File f = new File(Environment.getExternalStorageDirectory(),  System.currentTimeMillis()+".jpg");
        i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f)); 
        startActivityForResult(i, 0); 

}


}









No comments:

Post a Comment