package com.qk365.housekeeper.weiget;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.hardware.Camera.Size;
import android.media.MediaRecorder;
import android.media.MediaRecorder.OnErrorListener;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.qk365.housekeeper.R;
import com.qk365.housekeeper.entity.User;
import com.qk365.housekeeper.utils.SDCardUtils;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;
/**
* 视频录制控件
*
* @author lip
* @date 2015-3-16
*/
public class MovieRecorderView extends RelativeLayout implements OnErrorListener {
private SurfaceView mSurfaceView;
private SurfaceHolder mSurfaceHolder;
private MediaRecorder mMediaRecorder;
private Camera mCamera;
private int mWidth;// 视频分辨率宽度
private int mHeight;// 视频分辨率高度
private boolean isOpenCamera;// 是否一开始就打开摄像头
private int mRecordMaxTime;// 一次拍摄最长时间
private File mVecordFile = null;// 文件
private Context mContext;
private static final int COMPLETED = 1232;
private List<Size> mSupportedPreviewSizes;
private Size mPreviewSize;
private String vedioPathDir;
public MovieRecorderView(Context context) {
this(context, null);
}
public MovieRecorderView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public MovieRecorderView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mContext = context;
init();
}
/**
* 设置录制视频的保存路径
*/
public void setVedioDirPath(String pathDir) {
this.vedioPathDir = pathDir;
}
/**
* 录制完成回调接口
*
* @author lip
* @date 2015-3-16
*/
public interface OnRecordFinishListener {
public void onRecordFinish();
}
public void setOnRecordFinishListener(OnRecordFinishListener onRecordFinishListener) {
this.onRecordFinishListener = onRecordFinishListener;
}
private OnRecordFinishListener onRecordFinishListener;
private void init() {
mWidth = 320;
mHeight = 240;
mRecordMaxTime = 121; //最大录制时间120秒 2*60
isOpenCamera = true;
View moiveRecorderView = LayoutInflater.from(mContext).inflate(R.layout.moive_recorder_view, this);
mSurfaceView = (SurfaceView) moiveRecorderView.findViewById(R.id.surfaceview);
mSurfaceHolder = mSurfaceView.getHolder();
mSurfaceHolder.addCallback(new CustomCallBack());
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}
private class CustomCallBack implements Callback {
@Override
public void surfaceCreated(SurfaceHolder holder) {
//判断sd卡是否存在
if (!SDCardUtils.isSDCardEnable()) {
Toast.makeText(mContext, "sdcard 不存在!", Toast.LENGTH_SHORT).show();
}
if (!isOpenCamera)
return;
try {
initCamera();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
if (!isOpenCamera)
return;
freeCameraResource();
}
}
/**
* 初始化摄像头
*/
private boolean initCamera() {
try {
if (mCamera != null) {
freeCameraResource();
}
try {
mCamera = getCameraInstance();//1. 打开Camera
//设置摄像头以及摄像头的方向
// int CammeraIndex= CameraUtil.FindBackCamera();
// mCamera=Camera.open(CammeraIndex);
} catch (Exception e) {
e.printStackTrace();
freeCameraResource();
}
if (mCamera == null)
return false;
setCameraParams();
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(mSurfaceHolder); //2. 连接预览
mCamera.startPreview(); //3. 启动预览
// mCamera.cancelAutoFocus();// 2如果要实现连续的自动对焦,这一句必须加上
mCamera.unlock(); //4.a.给Camera对象解锁
} catch (Exception e) {
e.printStackTrace();
return false;
}
return true;
}
/**
* A safe way to get an instance of the Camera object.
*/
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
} catch (Exception e) {
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
/**
* 设置摄像头为横屏
*
* @author lip
* @date 2015-3-16
*/
private void setCameraParams() {
if (mCamera != null) {
Parameters mParams = mCamera.getParameters();
mParams.set("orientation", "portrait");
// mParams.set("orientation", "landscape");
mCamera.setParameters(mParams);
}
}
/**
* 释放摄像头资源
*
* @author liuyinjun
* @date 2015-2-5
*/
private void freeCameraResource() {
if (mCamera != null) {
try {
mCamera.setPreviewCallback(null);
// mCamera.setPreviewCallbackWithBuffer(null);
mCamera.stopPreview();
mCamera.lock();
mCamera.release();
mCamera = null;
} catch (Exception e) {
e.printStackTrace();
}
}
}
private void createRecordDir() {
File sampleDir = new File(vedioPathDir);
if (!sampleDir.exists()) {
sampleDir.mkdirs();
}
File vecordDir = sampleDir;
StringBuilder sb = new StringBuilder();
if (User.getInstance() != null)
sb.append(User.getInstance().getUseID());
int random = (int) (Math.random() * (99999 - 10000 + 1)) + 10000;// 获取5位随机数
sb.append(random);
sb.append("-");
sb.append(System.currentTimeMillis());
sb.append(".mp4");
// 创建文件
// mVecordFile = File.createTempFile(User.getInstance().getUseID() + "", ".mp4", vecordDir);//mp4格式
mVecordFile = new File(vecordDir, sb.toString());//mp4格式
//LogUtils.i(mVecordFile.getAbsolutePath());
Log.d("Path:", mVecordFile.getAbsolutePath());
}
/**
* 初始化录制
*/
private boolean initRecord() {
try {
if (mMediaRecorder == null) {
mMediaRecorder = new MediaRecorder(); // 创建MediaRecorder
}
mMediaRecorder.reset();
//mCamera为null打开mCamera
if (mCamera != null) {
mCamera.stopPreview();
}
initCamera