parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); }
mCamera.setParameters(parameters); }
int rotation =
getWindowManager().getDefaultDisplay().getRotation(); int orientation = orientations.get(rotation); mCamera.setDisplayOrientation(orientation); }
@Override
protected void onResume() { super.onResume(); initCamera(); }
@Override
public void onPause() { releaseCamera(); super.onPause(); }
/**
* 开始录制 */
private void startRecord() {
if (mRecorder == null) {
mRecorder = new MediaRecorder(); // 创建MediaRecorder }
if (mCamera != null) { mCamera.stopPreview(); mCamera.unlock();
mRecorder.setCamera(mCamera); } try {
// 设置音频采集方式
mRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); //设置视频的采集方式
mRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); //设置文件的输出格式
mRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);//aac_adif, aac_adts, output_format_rtp_avp, output_format_mpeg2ts ,webm //设置audio的编码格式
mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); //设置video的编码格式
mRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); //设置录制的视频编码比特率
mRecorder.setVideoEncodingBitRate(1024 * 1024); //设置录制的视频帧率,注意文档的说明: mRecorder.setVideoFrameRate(30); //设置要捕获的视频的宽度和高度
mSurfaceHolder.setFixedSize(320, 240);//最高只能设置640x480
mRecorder.setVideoSize(320, 240);//最高只能设置640x480 //设置记录会话的最大持续时间(毫秒) mRecorder.setMaxDuration(60 * 1000);
mRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); String path = getExternalCacheDir().getPath(); if (path != null) {
File dir = new File(path + \ if (!dir.exists()) { dir.mkdir(); }
path = dir + \\
//设置输出文件的路径
mRecorder.setOutputFile(path); //准备录制
mRecorder.prepare(); //开始录制
mRecorder.start(); isRecording = true;
btnStartStop.setText(\停止\ }
} catch (Exception e) { e.printStackTrace(); } } /**
* 停止录制 */
private void stopRecord() { try {
//停止录制
mRecorder.stop(); //重置
mRecorder.reset();
btnStartStop.setText(\开始\ } catch (Exception e) { e.printStackTrace(); }
isRecording = false; }
/**
* 释放MediaRecorder */
private void releaseMediaRecorder() { if (mRecorder != null) { mRecorder.release(); mRecorder = null; } }
/**
* 释放相机资源 */
private void releaseCamera() { try {
if (mCamera != null) {
mCamera.stopPreview();
mCamera.setPreviewCallback(null); mCamera.unlock(); mCamera.release(); }
} catch (RuntimeException e) { } finally {
mCamera = null; } }
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// 将holder,这个holder为开始在onCreate里面取得的holder,将它赋给mSurfaceHolder
mSurfaceHolder = holder; if (mCamera == null) { return; } try {
//设置显示
mCamera.setPreviewDisplay(holder); mCamera.startPreview(); } catch (Exception e) { e.printStackTrace(); releaseCamera(); finish(); } }
@Override
public void surfaceCreated(SurfaceHolder holder) {
// 将holder,这个holder为开始在onCreate里面取得的holder,将它赋给mSurfaceHolder
mSurfaceHolder = holder; }
@Override
public void surfaceDestroyed(SurfaceHolder holder) { // surfaceDestroyed的时候同时对象设置为null if (isRecording && mCamera != null) { mCamera.lock(); }
mSurfaceView = null; mSurfaceHolder = null; releaseMediaRecorder(); releaseCamera(); }
private class CameraSizeComparator implements Comparator
public int compare(Camera.Size lhs, Camera.Size rhs) { if (lhs.width == rhs.width) { return 0;
} else if (lhs.width > rhs.width) { return 1; } else {
return -1; } } } }
3.)布局文件
xmlns:android=\ xmlns:tools=\ android:layout_width=\ android:layout_height=\ android:orientation=\ android:id=\ android:layout_width=\ android:layout_height=\ android:layout_weight=\ />