網站首頁 編程語言 正文
本文實例為大家分享了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;
原文鏈接:https://blog.csdn.net/wangmaok/article/details/96972567
相關推薦
- 2022-05-23 C++單例模式的幾種實現方法詳解_C 語言
- 2021-12-15 Android?studio導出APP測試包和構建正式簽名包_Android
- 2022-06-08 FreeRTOS編碼標準及風格指南_操作系統
- 2022-04-21 Android中的LeakCanary的原理詳解_Android
- 2022-10-25 IDEA 安裝tomcat10創建servlet報404錯誤
- 2022-03-20 C語言初階之數組詳細介紹_C 語言
- 2022-04-19 詳解C語言的mem系列函數_C 語言
- 2022-03-28 Go實現用戶每日限額的方法(例一天只能領三次福利)_Golang
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支