Android 如何去限制和獲取攝像的時長,並且怎麼獲取視頻的第一幀
開啟視頻錄制,需要創建一個MediaRecorder對象,並調用Camera::unLock操作解鎖攝像頭,因為默認Camera都是鎖定的,只有解鎖後MediaRecorder等多媒體進程調用,並設置一些參數,然後調用MediaRecorder:: start開啟錄制具體可以參閱如下代碼:
MediaRecorder mMediaRecorder = new MediaRecorder();
// Unlock the camera object before passing it to media recorder.
camera.unlock();
mMediaRecorder.setCamera(camera);
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mMediaRecorder.setProfile(mProfile);
mMediaRecorder.setMaxDuration(100000);//ms為單位
long dateTaken = System.currentTimeMillis();
Date date = new Date(dateTaken);
SimpleDateFormat dateFormat = new SimpleDateFormat(getString(R.string.video_file_name_format));
String title = dateFormat.format(date);
String filename = title + ".3gp"; // Used when emailing.
String cameraDirPath = ImageManager.CAMERA_IMAGE_BUCKET_NAME;
String filePath = cameraDirPath + "/" + filename;
File cameraDir = new File(cameraDirPath);
cameraDir.mkdirs();
mMediaRecorder.setOutputFile(filePath);
try {
mMediaRecorder.prepare();
mMediaRecorder.start(); // Recording is now started
} catch (RuntimeException e) {
Log.e(TAG, "Could not start media recorder. ", e);
return;
}
上面設置了最大間隔為100s,當100是視頻錄制結束,錄制就會被停止,如果沒有設時長和文件大小限制,那麼通常需要調用MediaRecorder:: stop函數主動停止視頻的錄制,並將Camera對象通過lock函數繼續加鎖,示例代碼如下
mMediaRecorder.stop();
mMediaRecorder.reset();
mMediaRecorder.release();
mMediaRecorder = null;
if(camera != null)
camera.lock();
關於獲取視頻的第一幀可以參考下http://www.2cto.com/kf/201501/370235.html 《Android視頻處理 --處理視頻第一幀縮略圖》