上一篇camera前后攝像頭同時預(yù)覽的后續(xù),前后攝像頭同時錄像。
直接上重要的代碼就好然想。
/**
*初始化控件以及回調(diào)
*/
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;
// 錄像線程灌曙,當(dāng)然也可以在別的地方啟動,但是一定要在onCreate方法執(zhí)行完成以及surfaceHolder被賦值以后啟動
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
Log.i("SurfaceHolder", "surfaceDestroyed()");
// surfaceDestroyed的時候同時對象設(shè)置為null
surfaceview1 = null;
surfaceHolder1 = null;
/*釋放資源 mediarecorder mCamera 否則會后果很嚴(yán)重*/
if (thread1!=null) {
thread1.stopRecord();
thread1=null;
}
}
}); // holder加入回調(diào)接口
// 設(shè)置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;
// 錄像線程蚣驼,當(dāng)然也可以在別的地方啟動,但是一定要在onCreate方法執(zhí)行完成以及surfaceHolder被賦值以后啟動
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
// TODO Auto-generated method stub
Log.i("SurfaceHolder", "surfaceDestroyed()");
// surfaceDestroyed的時候同時對象設(shè)置為null
surfaceview2 = null;
surfaceHolder2 = null;
/*釋放資源 mediarecorder mCamera 否則會后果很嚴(yán)重*/
if (thread2!=null) {
thread2.stopRecord();
thread2=null;
}
}
}); // holder加入回調(diào)接口
// 設(shè)置setType
surfaceHolder2.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
接下來是兩個按鈕的點(diǎn)擊事件:
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);
/**
* 啟動定時器相艇,到規(guī)定時間recordTime后執(zhí)行停止錄像任務(wù)
*/
Timer timer = new Timer();
timer.schedule(new TimerThread(), recordTime);
}
public void startRecord(int McameraId) {
//自己動手豐衣足食
根據(jù)傳入?yún)?shù)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();
}
}