http://developer.android.com/guide/topics/media/camera.html
Note: It is possible to use MediaRecorder without creating a camera preview first and skip the first few steps of this process. However, since users typically prefer to see a preview before starting a recording, that process is not discussed here.
Android的文档如上,我想不创建一个照相机的预览界面,按理来说是可行的。我参考文档里的步骤,省去startpreview.核心代码如下
public void init(){
CameraManager manager = (CameraManager)getSystemService(Context.CAMERA_SERVICE);
Log.e(TAG, "OpenCamera-Video E");
String cameraId = null;
try {
if(!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)){
throw new RuntimeException("Time out waiting to lock Camera opening");
}
cameraId = manager.getCameraIdList()[0];
CameraCharacteristics characteristics = manager.getCameraCharacteristics(cameraId);
StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP);
mVideoSize = chooseVideoSize(map.getOutputSizes(MediaRecorder.class));
mMediaRecorder = new MediaRecorder();
manager.openCamera(cameraId,mStateCallback,null);
setUpMediaRecorder();
} catch (CameraAccessException e) {
e.printStackTrace();
Log.e(TAG,"cannot access the camera");
} catch (InterruptedException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
Log.e(TAG,"openCamera-video X");
}
private void setUpMediaRecorder() throws IOException {
mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE);
mMediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mMediaRecorder.setVideoEncodingBitRate(10000000);
mMediaRecorder.setVideoFrameRate(30);
mMediaRecorder.setVideoSize(mVideoSize.getWidth(), mVideoSize.getHeight());
mMediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC);
mMediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264);
mMediaRecorder.setOutputFile(String.valueOf(getVideoFile()));
//mMediaRecorder.setOrientationHint(orientation);
mMediaRecorder.prepare();
}
private CameraDevice.StateCallback mStateCallback = new CameraDevice.StateCallback() {
@Override
public void onOpened(CameraDevice cameraDevice) {
mCameraDevice = cameraDevice;
mCameraOpenCloseLock.release();
}
@Override
public void onDisconnected(CameraDevice cameraDevice) {
mCameraOpenCloseLock.release();
cameraDevice.close();
mCameraDevice = null;
}
@Override
public void onError(CameraDevice cameraDevice, int i) {
mCameraOpenCloseLock.release();
cameraDevice.close();
mCameraDevice = null;
}
};
private void startRecordingVideo() {
try {
// UI
// mButtonVideo.setText(R.string.stop);
mIsRecordingVideo = true;
Log.i("VideoService","在startRecordingVideo里");
// Start recording
mMediaRecorder.start();
} catch (IllegalStateException e) {
e.printStackTrace();
}
}
private boolean mIsRecordingVideo = false;
private void stopRecordingVideo() {
// UI
mIsRecordingVideo = false;
// mButtonVideo.setText(R.string.record);
// Stop recording
mMediaRecorder.stop();
mMediaRecorder.reset();
mMediaRecorder.release();
// Activity activity = getActivity();
Toast.makeText(this, "Video saved: " + getVideoFile(),
Toast.LENGTH_SHORT).show();
// startPreview();
}
在初始化的时候执行init()函数,之后,点击button按钮,对mIsRecordingVideo值判断,若为false,进入startRecordingVideo(),启动录像;若为true,进入stopRecordingVideo(),停止录像。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。