views:

39

answers:

0
  package com.VideoRecord;

import java.io.File;
import java.io.IOException;
import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Intent;
import android.media.MediaPlayer;
import android.media.MediaRecorder;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.Button;

public class VideoRecord extends Activity {
    /** Called when the activity is first created. */

    public MediaRecorder mrec = null;
    private Button startRecording = null;
    private Button stopRecording = null;
    private static final String TAG = "SoundRecordingDemo";
    File videofile;
    private SurfaceView sf;
    private SurfaceHolder previewHolder;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);



    startRecording = (Button)findViewById(R.id.startrecording);
      stopRecording = (Button)findViewById(R.id.stoprecording);

      sf = (SurfaceView) findViewById(R.id.sfpr);
      previewHolder = sf.getHolder();
      previewHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);



        startRecording.setOnClickListener(new View.OnClickListener(){
          public void onClick(View v) {
           try
           {
               startRecording.setEnabled(false);
               stopRecording.setEnabled(true);
               stopRecording.requestFocus();

               startRecording();
           }catch (Exception ee)
           {
               Log.e(TAG,"Caught io exception " + ee.getMessage());
           }

          }
        });

        stopRecording.setOnClickListener(new View.OnClickListener(){
          public void onClick(View v) {
              startRecording.setEnabled(true);
              stopRecording.setEnabled(false);
              startRecording.requestFocus();
              stopRecording();
              processvideofile();
          }

        });

       stopRecording.setEnabled(false);
       startRecording.setEnabled(true);

    }
    protected void processvideofile() {

        ContentValues values = new ContentValues(4);
        long current = System.currentTimeMillis();


        values.put(MediaStore.Video.Media.TITLE, "video" + videofile.getName());
        values.put(MediaStore.Video.Media.DATE_ADDED, (int) (current / 1000));
        values.put(MediaStore.Video.Media.MIME_TYPE, "audio/3gpp");
        values.put(MediaStore.Video.Media.DATA, videofile.getAbsolutePath());
        ContentResolver contentResolver = getContentResolver();

        Uri base = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        Uri newUri = contentResolver.insert(base, values);

        // this does not always seem to work cleanly....
        sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));



      }


    protected void startRecording() throws IOException 
    {
        mrec.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
        mrec.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
        mrec.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
//  mrec.setOutputFile(path);
        mrec.setVideoFrameRate(15);
        mrec.setVideoSize(176, 144);
        if (videofile == null) {
              File sampleDir = Environment.getExternalStorageDirectory();

              try { 
                  videofile = File.createTempFile("ived", ".3gp", sampleDir);
              } 
              catch (IOException e)
              {
                  Log.e(TAG,"sdcard access error");
                  return;
              }
        }

        mrec.setOutputFile(videofile.getAbsolutePath());

        mrec.prepare();
        mrec.start();

    }

    protected void stopRecording() {
        mrec.stop();
        mrec.reset();  
        mrec.release();
      }

   }