Android Open Source - camera Record Activity






From Project

Back to project page camera.

License

The source code is released under:

Apache License

If you think the Android project camera listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cn.bgxt.mediarecorderdemo;
/* w  w w  .jav  a 2  s.c  om*/
import java.io.File;
import android.app.Activity;
import android.media.MediaRecorder;
import android.media.MediaRecorder.OnErrorListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class RecordActivity extends Activity {
  private Button btn_RecordStart, btn_RecordStop;
  private MediaRecorder mediaRecorder;
  private boolean isRecording;
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_record);

    btn_RecordStart = (Button) findViewById(R.id.btn_RecordStart);
    btn_RecordStop = (Button) findViewById(R.id.btn_RecordStop);
    
    btn_RecordStop.setEnabled(false);
    
    btn_RecordStart.setOnClickListener(click);
    btn_RecordStop.setOnClickListener(click);
  }

  private View.OnClickListener click = new OnClickListener() {

    @Override
    public void onClick(View v) {
      switch (v.getId()) {
      case R.id.btn_RecordStart:
        start();
        break;
      case R.id.btn_RecordStop:
        stop();
        break;
      default:
        break;
      }
    }
  };

  /**
   * ?????
   */
  protected void start() {
    try {
      File file = new File("/mnt/emmc/DCIM/100MEDIA/mediarecorder.amr");
      if (file.exists()) {
        // ???????????????????????????????????????
        file.delete();
      }
      mediaRecorder = new MediaRecorder();
      // ??????????
      mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
      // ?????????????????
      mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
      // ??????????????
      mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
      // ????????????????????
      mediaRecorder.setOutputFile(file.getAbsolutePath());

      mediaRecorder.setOnErrorListener(new OnErrorListener() {
        
        @Override
        public void onError(MediaRecorder mr, int what, int extra) {
          // ???????????
          mediaRecorder.stop();
          mediaRecorder.release();
          mediaRecorder = null;
          isRecording=false;
          btn_RecordStart.setEnabled(true);
          btn_RecordStop.setEnabled(false);
          Toast.makeText(RecordActivity.this, "?????????", 0).show();
        }
      });
      
      // ????????
      mediaRecorder.prepare();
      mediaRecorder.start();
      
      isRecording=true;
      btn_RecordStart.setEnabled(false);
      btn_RecordStop.setEnabled(true);
      Toast.makeText(RecordActivity.this, "?????", 0).show();
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * ??????
   */
  protected void stop() {
    if (isRecording) {
      // ?????????????????????
      mediaRecorder.stop();
      mediaRecorder.release();
      mediaRecorder = null;
      isRecording=false;
      btn_RecordStart.setEnabled(true);
      btn_RecordStop.setEnabled(false);
      Toast.makeText(RecordActivity.this, "??????", 0).show();
    }
  }

  @Override
  protected void onDestroy() {
    if (isRecording) {
      // ?????????????????????
      mediaRecorder.stop();
      mediaRecorder.release();
      mediaRecorder = null;
    }
    super.onDestroy();
  }

}




Java Source Code List

cn.bgxt.mediarecorderdemo.MainActivity.java
cn.bgxt.mediarecorderdemo.RecordActivity.java
com.example.android.photobyintent.AlbumStorageDirFactory.java
com.example.android.photobyintent.BaseAlbumDirFactory.java
com.example.android.photobyintent.FroyoAlbumDirFactory.java
com.example.android.photobyintent.PhotoIntentActivity.java
com.marcodinacci.book.acb.VideoActivity.java
org.yanzi.activity.CameraActivity.java
org.yanzi.camera.CameraInterface.java
org.yanzi.camera.preview.CameraTextureView.java
org.yanzi.util.CamParaUtil.java
org.yanzi.util.DisplayUtil.java
org.yanzi.util.FileUtil.java
org.yanzi.util.ImageUtil.java