上一篇camera前后摄像头同时预览的后续,前后摄像头同时录像。
直接上重要的代码就好。
/**
*初始化控件以及回调
*/
private void init() {
surfaceview1 = (SurfaceView) this.findViewById(R.id.surfaceview1);
surfaceview2 = (SurfaceView) this.findViewById(R.id.surfaceview2);
lay = (LinearLayout) this.findViewById(R.id.lay1);
lay = (LinearLayout) this.findViewById(R.id.lay2);
start = (Button) this.findViewById(R.id.start);
stop = (Button) this.findViewById(R.id.stop);
//lay.setVisibility(LinearLayout.INVISIBLE);
surfaceHolder1 = this.surfaceview1.getHolder();// 取得holder
surfaceHolder1.addCallback(new Callback() {
@Override
public void surfaceChanged(SurfaceHolder holder1, int format, int width, int height) {
// TODO Auto-generated method stub
// 将holder,这个holder为开始在oncreat里面取得的holder,将它赋给surfaceHolder
Log.i("SurfaceHolder", "surfaceChanged()");
surfaceHolder1 = holder1;
}
@Override
public void surfaceCreated(SurfaceHolder holder1) {
// TODO Auto-generated method stub
Log.i("SurfaceHolder", Thread.currentThread().getName());
// 将holder,这个holder为开始在oncreat里面取得的holder,将它赋给surfaceHolder
surfaceHolder1 = holder1;
// 录像线程,当然也可以在别的地方启动,但是一定要在onCreate方法执行完成以及surfaceHolder被赋值以后启动
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
Log.i("SurfaceHolder", "surfaceDestroyed()");
// surfaceDestroyed的时候同时对象设置为null
surfaceview1 = null;
surfaceHolder1 = null;
/*释放资源 mediarecorder mCamera 否则会后果很严重*/
if (thread1!=null) {
thread1.stopRecord();
thread1=null;
}
}
}); // holder加入回调接口
// 设置setType
surfaceHolder1.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
surfaceHolder2 = this.surfaceview2.getHolder();// 取得holder
surfaceHolder2.addCallback(new Callback() {
@Override
public void surfaceChanged(SurfaceHolder holder2, int format, int width, int height) {
// TODO Auto-generated method stub
// 将holder,这个holder为开始在oncreat里面取得的holder,将它赋给surfaceHolder
Log.i("SurfaceHolder", "surfaceChanged()");
surfaceHolder2 = holder2;
}
@Override
public void surfaceCreated(SurfaceHolder holder2) {
// TODO Auto-generated method stub
Log.i("SurfaceHolder", Thread.currentThread().getName());
// 将holder,这个holder为开始在oncreat里面取得的holder,将它赋给surfaceHolder
surfaceHolder2 = holder2;
// 录像线程,当然也可以在别的地方启动,但是一定要在onCreate方法执行完成以及surfaceHolder被赋值以后启动
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
Log.i("SurfaceHolder", "surfaceDestroyed()");
// surfaceDestroyed的时候同时对象设置为null
surfaceview2 = null;
surfaceHolder2 = null;
/*释放资源 mediarecorder mCamera 否则会后果很严重*/
if (thread2!=null) {
thread2.stopRecord();
thread2=null;
}
}
}); // holder加入回调接口
// 设置setType
surfaceHolder2.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
接下来是两个按钮的点击事件:
start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (thread1==null) {
thread1 = new RecordThread(10*60*1000, surfaceview1, surfaceHolder1,66);
thread1.start();
thread2 = new RecordThread(10*60*1000, surfaceview2, surfaceHolder2,88);
thread2.start();
}else {
Toast.makeText(MainActivity.this, "正在录制中……", Toast.LENGTH_SHORT).show();
}
}
});
stop.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (thread1 != null || thread2 != null) {
thread1.stopRecord();
thread1=null;
thread2.stopRecord();
thread2=null;
}else {
Toast.makeText(MainActivity.this, "视频录制还没开始", Toast.LENGTH_SHORT).show();
}
}
});
录像的线程RecordThread.java
public RecordThread(long recordTime, SurfaceView surfaceview,
SurfaceHolder surfaceHolder,int camId) {
this.recordTime = recordTime;//定时时间
this.surfaceview = surfaceview;
this.surfaceHolder = surfaceHolder;
this.Mcameraid = camId;
}
public voidrun(){
/**
* 开始录像
*/
startRecord(Mcameraid);
/**
* 启动定时器,到规定时间recordTime后执行停止录像任务
*/
Timer timer = new Timer();
timer.schedule(new TimerThread(), recordTime);
}
public void startRecord(int McameraId) {
//自己动手丰衣足食
根据传入参数McameraId判断,用open(cameraid)开启前后摄像头,启动MediaRecorder录像
}
/**
*停止录制
*/
public void stopRecord() {
Log.d("lyb","stopRecord()");
surfaceview = null;
surfaceHolder = null;
if (mediarecorder != null) {
// 停止录制
mediarecorder.stop();
mediarecorder.reset();
// 释放资源
mediarecorder.release();
mediarecorder = null;
if (mCamera != null) {
mCamera.release();
mCamera = null;
}
}
}
/**
*定时器
*
* @author bcaiw
*
*/
class TimerThread extends TimerTask {
/**
* 停止录像
*/
@Override
public void run() {
stopRecord();
this.cancel();
}
}
网友评论