public class RecordVideoActivity extends ActionBarActivity {
private static final String TAG = "RecordVideo";
private SurfaceView surfaceView;
private MediaRecorder mediaRecorder;
private boolean record;
private TextView testusername;
private Camera camera;
// 计时器相关
private MyChronograph myChronograph;
private TextView chronograph = null;
private ProgressBar schedule;
private boolean recordOver = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.recordvideo);
setTitle("录制视频");
mediaRecorder = new MediaRecorder();
surfaceView = (SurfaceView) this.findViewById(R.id.surfaceView);
this.surfaceView.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
this.surfaceView.getHolder().setFixedSize(320, 240);//设置分辨率
testusername = (EditText)findViewById(R.id.rv_testusername);
chronograph = (TextView)findViewById(R.id.rv_record_time);
schedule = (ProgressBar)findViewById(R.id.rv_schedule);
schedule.setMax(60);
ButtonClickListener listener = new ButtonClickListener();
Button stopButton = (Button) this.findViewById(R.id.rv_stop);
Button recordButton = (Button) this.findViewById(R.id.rv_record);
stopButton.setOnClickListener(listener);
recordButton.setOnClickListener(listener);
}
@Override
protected void onDestroy() {
// TODO Auto-generated method stub
if(mediaRecorder!=null)
mediaRecorder.release();
super.onDestroy();
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
}
@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
}
private final class ButtonClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
if(!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Toast.makeText(RecordVideoActivity.this, "木有检测到SD扩展卡", 1).show();
return ;
}
try {
switch (v.getId()) {
case R.id.rv_record:
// 要求输入用户名
String testuser;
if(testusername.getText()==null || testusername.getText().toString().equals("")){
Toast.makeText(RecordVideoActivity.this, "请输入测试者姓名", Toast.LENGTH_LONG).show();
return;
}
Log.i(TAG,"检测通过");
recordOver = false;
testuser = testusername.getText().toString();
testuser = android.os.Build.MODEL + "-" + testuser;
mediaRecorder.reset();
if(isSurportFlashlight(RecordVideoActivity.this)){
if (camera == null)
camera = Camera.open();
Camera.Parameters myParameters = camera.getParameters();
myParameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(myParameters);
camera.startPreview();
camera.unlock();
mediaRecorder.setCamera(camera);
}
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
mediaRecorder.setVideoSize(320, 240);
mediaRecorder.setVideoFrameRate(30); //每秒30帧
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H263);
//mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
SimpleDateFormat ff = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
String recordTimeString = String.valueOf(ff.format(System.currentTimeMillis()));
File videoFile = IOUtil.CreateNewFile(Environment.getExternalStorageDirectory().getPath()+"/phonedoctor/video",
testuser + "-" + recordTimeString+".3gp",null);
mediaRecorder.setOutputFile(videoFile.getAbsolutePath());
mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface());
mediaRecorder.prepare();
mediaRecorder.start(); // 开始录制
// 开启计时线程
myChronograph = new MyChronograph(mHandler,60000);
myChronograph.start();
Toast.makeText(RecordVideoActivity.this, "开始录制视频!", Toast.LENGTH_SHORT).show();
record = true;
((Button)findViewById(R.id.rv_record)).setEnabled(false);
break;