小编给大家分享一下Android如何实现调用手机摄像头录像限制录像时长,希望大家阅读完这篇文章之后都有所收获,下面让我们一起去探讨吧!
具体内容如下
因为服务器空间有限,所以视频时长必须有所限制。
在xml中先布局一个按钮,点击开始录频。布局一个TextView用于显示倒计时的时间。
一、使用Hander+TimerTask完成定时操作
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; }
四、配置MediaRecorder
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;
看完了这篇文章,相信你对“Android如何实现调用手机摄像头录像限制录像时长”有了一定的了解,如果想了解更多相关知识,欢迎关注亿速云行业资讯频道,感谢各位的阅读!
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。