Android 中使用MediaRecorder进行录像详解(视频录制)

package com.iaiai.video.test;

import java.io.File;
import java.io.IOException;

import android.app.Activity;
import android.hardware.Camera;
import android.media.MediaRecorder;
import android.media.MediaRecorder.AudioEncoder;
import android.media.MediaRecorder.AudioSource;
import android.media.MediaRecorder.OnErrorListener;
import android.media.MediaRecorder.OutputFormat;
import android.media.MediaRecorder.VideoEncoder;
import android.media.MediaRecorder.VideoSource;
import android.os.Bundle;
import android.os.Environment;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

/**  
 * qq 176291935
 * @author iaiai
 * @version 
 */
public class MainActivity extends Activity {

	private SurfaceView surfaceView;
	private SurfaceHolder mSurfaceHolder;

	private MediaRecorder mMediaRecorder;

	private int mWidth = 320;// 视频分辨率宽度
	private int mHeight = 240;// 视频分辨率高度
	
	private Camera mCamera;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		surfaceView = (SurfaceView) findViewById(R.id.sView);
		mSurfaceHolder = surfaceView.getHolder();// 取得holder
		mSurfaceHolder.addCallback(new Callback() {
			@Override
			public void surfaceDestroyed(SurfaceHolder holder) {
				closeVideo();
				freeCameraResource();
			}

			@Override
			public void surfaceCreated(SurfaceHolder holder) {
				mSurfaceHolder = holder;
				
				try {
					initCamera();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}

			@Override
			public void surfaceChanged(SurfaceHolder holder, int format,
					int width, int height) {
				mSurfaceHolder = holder;
			}
		});
		
		mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}
	
	/**
     * 初始化摄像头
     *
     * @author liuyinjun
     * @date 2015-2-5
     * @throws IOException
     */
    private void initCamera() throws IOException {
        if (mCamera != null) {
            freeCameraResource();
        }
        try {
            mCamera = Camera.open();
        } catch (Exception e) {
            e.printStackTrace();
            freeCameraResource();
        }
        if (mCamera == null)
            return;

        // setCameraParams();
        mCamera.setDisplayOrientation(90);
        mCamera.setPreviewDisplay(mSurfaceHolder);
        mCamera.startPreview();
        mCamera.unlock();
        
        initRecord();
    }

	/**
	 * 初始化
	 * 
	 * @author liuyinjun
	 * @date 2015-2-5
	 * @throws IOException
	 */
	private void initRecord() throws IOException {
		mMediaRecorder = new MediaRecorder();
		mMediaRecorder.reset();
		if (mCamera != null)
            mMediaRecorder.setCamera(mCamera);
		mMediaRecorder.setOnErrorListener(new OnErrorListener() {
			@Override
			public void onError(MediaRecorder mr, int what, int extra) {
				try {
					if (mr != null)
						mr.reset();
				} catch (IllegalStateException e) {
					e.printStackTrace();
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
		mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
		mMediaRecorder.setVideoSource(VideoSource.CAMERA);// 视频源
		mMediaRecorder.setAudioSource(AudioSource.MIC);// 音频源
		mMediaRecorder.setOutputFormat(OutputFormat.MPEG_4);// 视频输出格式
		mMediaRecorder.setAudioEncoder(AudioEncoder.AMR_NB);// 音频格式

		// CamcorderProfile profile =
		// CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
		// Log.i("********",profile.videoFrameWidth+"********"+profile.videoFrameHeight);

		mMediaRecorder.setVideoSize(mWidth, mHeight);// 设置分辨率:
		mMediaRecorder.setVideoFrameRate(5);// 每秒5帧
		mMediaRecorder.setVideoEncodingBitRate(1 * 1280 * 720);// 设置帧频率,然后就清晰了
		mMediaRecorder.setOrientationHint(90);// 输出旋转90度,保持竖屏录制
		mMediaRecorder.setVideoEncoder(VideoEncoder.MPEG_4_SP);// 视频录制格式
		// mediaRecorder.setMaxDuration(Constant.MAXVEDIOTIME * 1000);
		mMediaRecorder.setOutputFile(getFile());
		mMediaRecorder.prepare();
		try {
			mMediaRecorder.start();
		} catch (IllegalStateException e) {
			e.printStackTrace();
		} catch (RuntimeException e) {
			e.printStackTrace();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private String getFile() {
		try {
			File viodFile = new File(Environment.getExternalStorageDirectory()
					.getCanonicalFile() + "/dc/myvideo.mp4");
			if (!viodFile.exists())
				viodFile.createNewFile();

			return viodFile.getAbsolutePath();
		} catch (IOException e1) {
			e1.printStackTrace();

			return null;
		}
	}
	
	/**
     * 释放摄像头资源
     *
     * @author liuyinjun
     * @date 2015-2-5
     */
    private void freeCameraResource() {
        if (mCamera != null) {
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();
            mCamera.lock();
            mCamera.release();
            mCamera = null;
        }
    }

	@Override
	protected void onDestroy() {
		super.onDestroy();

		closeVideo();
		freeCameraResource();
	}
	
	private void closeVideo(){
		if (mMediaRecorder != null) {  
            // 停止录制  
			mMediaRecorder.stop();  
            // 释放资源  
			mMediaRecorder.release();  
			mMediaRecorder = null;  
        }
		
		surfaceView = null;  
		mSurfaceHolder = null;  
		mMediaRecorder = null;
	}

}


xml没别的东西就放了一个SurfaceView

你可能感兴趣的:(Android 中使用MediaRecorder进行录像详解(视频录制))