因为服务器空间有限,所以视频时长必须有所限制。
在xml中先布局一个按钮,点击开始录频。布局一个TextView用于显示倒计时的时间。
private TextView btn_stop;
private int recLen = 11;
Timer timer = new Timer();
//时间倒计时放在开始按钮事件后,看标题7那里是时间倒计时开始的时候
//timer.schedule(task,1000,1000);
final Handler handler = new Handler(){
@Override
public void handleMessage(Message msg){
switch (msg.what){
case 1:
btn_stop.setText(""+recLen);
if(recLen<0){
timer.cancel();
btn_stop.setVisibility(View.GONE);
}
}
}
};
TimerTask task = new TimerTask() {
@Override
public void run() {
recLen--;
Message message = new Message();
message.what = 1;
handler.sendMessage(message);
//当倒计时时间到1的时候关闭录像
if (recLen == 1){
customTimer.Stop();
stopRecord();
}
}
};
private void initCamera() {
mCamera = Camera.open(0); //①
mCamera.setDisplayOrientation(90);
try {
mCamera.setPreviewDisplay(mSurfaceHolder);
mCamera.cancelAutoFocus();//此句加上 可自动聚焦 必须加
Camera.Parameters parameters = mCamera.getParameters();
//查询摄像头支持的分辨率
parameters.getSupportedPreviewSizes();
for (int i = 0; i < parameters.getSupportedPreviewSizes().size(); i++) {
Log.i("<><><><>Width", parameters.getSupportedPreviewSizes().get(i).width + "");
Log.i("<><><><>Height", parameters.getSupportedPreviewSizes().get(i).height + "");
}
//设置分辨率
parameters.setPreviewSize(1280, 720);
//设置聚焦模式
parameters.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
//缩短Recording启动时间
parameters.setRecordingHint(true);
//是否支持影像稳定能力,支持则开启
if (parameters.isVideoStabilizationSupported())
parameters.setVideoStabilization(true);
mCamera.setParameters(parameters);
mCamera.startPreview();
} catch (Exception e) {
Log.i(TAG, "Error starting camera preview: " + e.getMessage());
}
}
private boolean createRecordDir() {
if (!Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
Toast.makeText(this, "SD卡不存在!", Toast.LENGTH_SHORT).show();
return false;
}
File sampleDir = new File("/sdcard/myVideo/");
if (!sampleDir.exists()) {
sampleDir.mkdirs();
}
videoName = "VID_" + DateUtils.getData(DateUtils.FORMAT_YYYYMMDDHHMMSS) + ".mp4";
mVecordFile = new File(sampleDir, videoName);
return true;
}
private void setConfigRecord() {
mediaRecorder = new MediaRecorder();
mediaRecorder.reset();
mediaRecorder.setCamera(mCamera);
mediaRecorder.setOnErrorListener(onErrorListener);
//录像角度
mediaRecorder.setOrientationHint(90);
//使用SurfaceView预览
mediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface());
//1.设置采集声音
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
//设置采集图像
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
//2.设置视频,音频的输出格式 mp4
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.DEFAULT);
//3.设置音频的编码格式
mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
//设置图像的编码格式
mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
CamcorderProfile mProfile = CamcorderProfile.get(CamcorderProfile.QUALITY_480P);
mediaRecorder.setAudioEncodingBitRate(44100);
if (mProfile.videoBitRate > 2 * 1024 * 1024) {
mediaRecorder.setVideoEncodingBitRate(2 * 1024 * 1024);
} else {
mediaRecorder.setVideoEncodingBitRate(1024 * 1024);
}
mediaRecorder.setVideoFrameRate(mProfile.videoFrameRate);
mediaRecorder.setVideoSize(1280, 720);
mediaRecorder.setOutputFile(mVecordFile.getAbsolutePath());
}
private void stopRecord() {
try {
if (isRecording && mediaRecorder != null) {
mediaRecorder.setOnErrorListener(null);
mediaRecorder.setPreviewDisplay(null);
mediaRecorder.stop();
mediaRecorder.reset();
mediaRecorder.release();
mediaRecorder = null;
isRecording = false;
Log.i(TAG, "" + mVecordFile.toString());
//new imageTask().execute(mVecordFile);
MessageData md = new MessageData();
md.srdatatype = SRDataType.录像返回;
md.Data = mVecordFile.getAbsolutePath();
Intent intent = new Intent(BroadCastParameter.action);
intent.putExtra("data", md);
sendBroadcast(intent);
// Fragment页面广播通知
LocalBroadcastManager.getInstance(CustomRecorder.this).sendBroadcast(intent);
finish();
}
}catch (Exception ex)
{
//Toast.makeText(getApplicationContext(),ex.getMessage(),1).show();
}
}
private void stopCamera() {
if (mCamera != null) {
mCamera.setPreviewCallback(null);
mCamera.stopPreview();
mCamera.release();
mCamera = null;
}
}
public void onViewClicked(View view) {
if (Build.VERSION.SDK_INT >= 23) {
int REQUEST_CODE_CONTACT = 101;
//验证是否许可权限
for (String str : permissions) {
if (this.checkSelfPermission(str) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(getApplicationContext(),"存储权限未授权",1).show();
return;
}
}
}
switch (view.getId()) {
//开始
case R.id.btn_start:
btn_start.setVisibility(View.GONE);
btn_stop.setVisibility(View.VISIBLE);
//这是是判断视频文件有没有创建,如果没有就返回
boolean creakOk = createRecordDir();
if (!creakOk) {
return;
}
try {
mCamera.unlock();
setConfigRecord();
mediaRecorder.prepare();
mediaRecorder.start();
} catch (Exception e) {
//Toast.makeText(getApplicationContext(),e.getMessage(),Toast.LENGTH_LONG).show();
}
StartTime = new Date(System.currentTimeMillis());
isRecording = true;
customTimer.ReStart();
timer.schedule(task,1000,1000);
break;