|
由于在項(xiàng)目中要加入視頻拍攝 第一次使用了系統(tǒng) intent 方法 Intent intent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE); // String currentVideoName = System.currentTimeMillis() + ".mp4"; // File dir = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/xxx/"); // File out = new File(dir, currentVideoName); // PreferencesUtils.getInstance(this).put("REQUEST_VIDEO", out.getAbsolutePath()); // Uri uri = Uri.fromFile(out); // intent.putExtra(MediaStore.EXTRA_OUTPUT, uri); // //限制時(shí)長 // intent.putExtra(MediaStore.EXTRA_DURATION_LIMIT, 6); // //0和1是所有相機(jī)都有的設(shè)置,0是最小,1是最大,不存在中間0.5 // intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 0); // //限制大小 // intent.putExtra(MediaStore.EXTRA_SIZE_LIMIT, 1024*1024L); 該方法使用簡單 但是畫面質(zhì)量 只有0 和 1 2種 如果是1 1秒大概在2m 6秒的視頻12m左右 上傳和播放都會(huì)延遲 如果用第三方進(jìn)行壓縮 也比較耗時(shí) 換了一種方式MediaRecorder
mMediaRecorder = new MediaRecorder(); mMediaRecorder.reset(); if (mCamera != null) mMediaRecorder.setCamera(mCamera); mMediaRecorder.setOnErrorListener(this); mMediaRecorder.setPreviewDisplay(mSurfaceHolder.getSurface()); mMediaRecorder.setVideoSource(VideoSource.CAMERA);// 視頻源 mMediaRecorder.setAudioSource(AudioSource.MIC);// 音頻源 mMediaRecorder.setOutputFormat(OutputFormat.MPEG_4);// 視頻輸出格式 mMediaRecorder.setAudioEncoder(AudioEncoder.AMR_NB);// 音頻格式 mMediaRecorder.setVideoSize(mWidth, mHeight);// 設(shè)置分辨率: //setVideoSize需要權(quán)衡的因素較多,主要包括三方面:MediaRecorder支持的錄制尺寸、 // 視頻文件的大小以及兼容不同Android機(jī)型。這里采用640 * 480(微信小視頻的尺寸是320*240), // 文件大小在500-1000kb之間,并且市面上99%以上機(jī)型支持此錄制尺寸。 mMediaRecorder.setVideoSize(640, 480); // 設(shè)置錄制的視頻幀率。必須放在設(shè)置編碼和格式的后面,否則報(bào)錯(cuò) // mMediaRecorder.setVideoFrameRate(16);// 這個(gè)我把它去掉了,感覺沒什么用 mMediaRecorder.setVideoEncodingBitRate(1 * 1024 * 1024);// 設(shè)置幀頻率,然后就清晰了 肯定文件越大越清晰 mMediaRecorder.setOrientationHint(90);// 輸出旋轉(zhuǎn)90度,保持豎屏錄制 mMediaRecorder.setVideoEncoder(VideoEncoder.MPEG_4_SP);// 視頻錄制格式 // mediaRecorder.setMaxDuration(Constant.MAXVEDIOTIME * 1000); mMediaRecorder.setOutputFile(mRecordFile.getAbsolutePath()); mMediaRecorder.prepare(); try { mMediaRecorder.start(); } catch (IllegalStateException e) { e.printStackTrace(); } catch (RuntimeException e) { e.printStackTrace(); } catch (Exception e) { e.printStackTrace(); }
使用這個(gè)的好處是可以再拍攝之前設(shè)置參數(shù) 畫面的幀數(shù) 尺寸 可以有效限制文件大小 10秒的視頻在1m左右 只需要注意權(quán)限文件就可以了
|