|
本帖最后由 xiaoming 于 2016-1-11 11:40 编辑
CustomVideoRecord.java中存在问题,播放的总时间比录制时候的世间短,能提供最新的修改代码吗
public void startRecordVideo(int type) {
synchronized (this) {
try {
this.type = type;
startRecording = true;
isFirstH264 = true;
startTime = (new Date()).getTime();
sumFrame = 0;
sum = 0;
dataBuff = new LinkedList<VideoRecordBean>();
Log.d("tag", "start record video");
mThread = new Thread(new myRunnable());
mThread.start();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public void stopRecordVideo() {
synchronized (this) {
startRecording = false;
long time = (new Date()).getTime();
videoSumTime = (int) (time - startTime);
}
}
private class myRunnable implements Runnable {
@Override
public void run() {
try {
File div = new File("/sdcard/video");
if (!div.exists()) {
div.mkdirs();
}
String strDate = getStrDate();
String p = strDate + "_" + strDID + ".mp4";
File file = new File(div, p);
Log.d("tag",
"start record video fileName:"
+ file.getAbsolutePath());
videopath = file.getAbsolutePath();
outStream = new FileOutputStream(new File(videopath), true);
Log.d("tag", "���� type:" + type);
switch (type) {
case 1:// fileHeader 1:h264����
byte[] header = intToByte(1);
outStream.write(header);
break;
case 2: // fileHeader 2:jpg����
Log.d("tag", "jpg����");
byte[] header2 = intToByte(2);
outStream.write(header2);
break;
default:
break;
}
Log.d("tag", "outStream:" + outStream);
while (true && startRecording) {
if (dataBuff.size() > 0) {
sumFrame++;
Log.d("tag", "�ܹ�¼��" + sumFrame + " ֡ ���ļ���д��һ֡");
VideoRecordBean bean = dataBuff.poll();
if (type == 1) {// h264����
int size = bean.getWidth();
int t = bean.getType();
int tspan = bean.getTspan();
byte[] sizebyte = intToByte(size);
byte[] typebyte = intToByte(t);
byte[] picture = bean.getPicture();
byte[] time = intToByte(tspan);
Log.i("tag", "------------------2222222222222");
outStream.write(sizebyte);
outStream.write(typebyte);
outStream.write(time);
outStream.write(picture);
sum += tspan;
} else {// jpg����
byte[] picture = bean.getPicture();
byte[] length = intToByte(picture.length);
int tspan = bean.getTspan();
byte[] time = intToByte(tspan);
Log.d("tag", "¼��ʱ��֡ʱ��� Tspan: " + tspan);
sum += tspan;
outStream.write(length);
outStream.write(time);
outStream.write(picture);
}
outStream.flush();
} else {
Log.d("tag", "没有图片 暂停1秒");
Thread.sleep(1000);
}
}
Log.d("tag", "录制总时间 videoSumTime:" + videoSumTime);
Log.d("tag", "一共总的帧数:sumFrame = " + sumFrame);
byte[] endBytes = intToByte(videoEnd);
byte[] sumTimeBytes = intToByte(sum);
outStream.write(endBytes);
outStream.write(sumTimeBytes);
outStream.flush();
// 清除缓存
dataBuff.clear();
dataBuff = null;
// 向数据库中写入录像文件记录
Log.d("tag", "向数据库中写入录像文件记录");
dbUtil.open();
dbUtil.createVideoOrPic(strDID, videopath,
DatabaseUtil.TYPE_VIDEO, "2323");
dbUtil.close();
if (outStream != null) {
outStream.close();
outStream = null;
}
} catch (Exception e) {
Log.d("tag", "保存录像文件异常:" + e.getMessage());
e.printStackTrace();
}
}
};录制视频1分钟,但播放的时候只看到总时间40秒
|
|