Click here to Skip to main content
15,888,802 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi,

I need to create a video recorder application. In this case the current speed taken from GPS and other set of icons need to be shown in the video preview. Is this possible? I saw similar app "aLapRecorder" in the market place. I would be thankful if any one can share a source code or method to achieve this requirement.

- Thanks


This is the code I have try to Up to now:
C#
package com.tcs.video;

import android.app.Activity;
import android.content.Context;
import android.content.pm.ActivityInfo;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.widget.Toast;
public class VideoRecorder extends Activity{
    //Create objects of MediaRecorder and Preview class
    private MediaRecorder recorder;
    private Preview mPreview;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    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);
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        try{

            recorder = new MediaRecorder();
            //CamcorderProfile cpHigh = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
            //recorder.setProfile(cpHigh);

            mPreview = new Preview(this, recorder);
            //mPreview.setBackgroundResource(R.drawable.icon);
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
            setContentView(mPreview);
        }
        catch (Exception e) {
                // TODO: handle exception
            Log.v("onCreate", "Message:"+e.getMessage());
            Toast _display = Toast.makeText(this, "onCreate:"+e.getMessage(), Toast.LENGTH_SHORT);
            _display.show();
        }
   }
     /*!
    <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;
        Context _context;
        //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);
            _context = context;
            tempRecorder=recorder;
            try{
                mHolder=getHolder();
                mHolder.addCallback(this);
                mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

                // TODO Auto-generated constructor stub
            }
            catch (Exception e) {
                // TODO: handle exception
                Log.v("Preview Const 3", "Message:"+e.getMessage());
                Toast _display = Toast.makeText(_context, "Preview Const 3:"+e.getMessage(), Toast.LENGTH_SHORT);
                _display.show();
            }
        }
        @Override
        public SurfaceHolder getHolder() {
            // TODO Auto-generated method stub
            return super.getHolder();
        }
        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){
            try{
                tempRecorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
                tempRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
                tempRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP);
                tempRecorder.setVideoFrameRate(15);
                Display display = getWindowManager().getDefaultDisplay();
                int width =display.getWidth();
                int height =display.getHeight();
                tempRecorder.setVideoSize(width, height);
                tempRecorder.setOutputFile("/sdcard/recordvideooutput.3gp");
                tempRecorder.setPreviewDisplay(mHolder.getSurface());
                boolean isprepared = false;
                //while(!isprepared){
                //  try{
                        tempRecorder.prepare();
                        isprepared = true;
                //  }
                //  catch (Exception e) {
                //      // TODO: handle exception
                //  }
                //}
            } catch (Exception e) {
                //tempRecorder.release();
                //tempRecorder = null;
                Log.v("surfaceCreated", "Message:"+e.getMessage());
                Toast _display = Toast.makeText(_context, "surfaceCreated:"+e.getMessage(), Toast.LENGTH_SHORT);
                _display.show();
            }
        }
        public void surfaceDestroyed(SurfaceHolder holder)
        {
            try{
                if(tempRecorder!=null)
                {
                    tempRecorder.release();
                    tempRecorder = null;
                }
            }
            catch (Exception e) {
                // TODO: handle exception
                Log.v("surfaceDestroyed", "Message:"+e.getMessage());
                Toast _display = Toast.makeText(_context, "surfaceDestroyed:"+e.getMessage(), Toast.LENGTH_SHORT);
                _display.show();
            }
        }
        public void surfaceChanged(SurfaceHolder holder, int format, int w, int h)
        {
            // Now that the size is known, set up the camera parameters and begin
            // the preview.
        }

    }
}
Posted
Updated 15-May-11 7:03am
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900