您好,登錄后才能下訂單哦!
小編給大家分享一下Android如何實現(xiàn)調(diào)用手機攝像頭錄像限制錄像時長,希望大家閱讀完這篇文章之后都有所收獲,下面讓我們一起去探討吧!
具體內(nèi)容如下
因為服務器空間有限,所以視頻時長必須有所限制。
在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); //當?shù)褂嫊r時間到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); //是否支持影像穩(wěn)定能力,支持則開啟 if (parameters.isVideoStabilizationSupported()) parameters.setVideoStabilization(true); mCamera.setParameters(parameters); mCamera.startPreview(); } catch (Exception e) { Log.i(TAG, "Error starting camera preview: " + e.getMessage()); } }
三、創(chuàng)建視頻文件
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); //這是是判斷視頻文件有沒有創(chuàng)建,如果沒有就返回 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如何實現(xiàn)調(diào)用手機攝像頭錄像限制錄像時長”有了一定的了解,如果想了解更多相關知識,歡迎關注億速云行業(yè)資訊頻道,感謝各位的閱讀!
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權內(nèi)容。