我正在將照片/視頻捕獲集成到我的應用中,並且遇到了視頻捕獲問題。無論何時錄像開始,屏幕閃爍,我會暫停一段時間,然後開始錄像。但是,使用手機的相機應用程序,根本沒有閃爍/暫停。開始錄製視頻時屏幕閃爍並調整大小
而且,我的相機預覽顯示會在調用recorder.start()
後立即調整大小。我不明白這是爲什麼。它會使預覽失真(一切看起來都會變得更加寬廣)。
我的問題:如何防止開始錄像時的閃爍/暫停?如何防止recorder.start()
調整我的預覽顯示?
每當啓用「視頻模式」時,立即調用initRecording()
。一旦用戶按下按鈕,startRecording()
被調用。最後,當再次按下按鈕時,調用stopRecording()
。不太重要的是,當切換回「圖片模式」時,destroyRecorder()
被調用。
@Override
public void onResume() {
super.onResume();
Camera camera = null;
try {
camera = Camera.open();
}
catch (Exception e) {
// Camera isn't available
Toast.makeText(getActivity(), "Camera is not available at this time.", Toast.LENGTH_SHORT).show();
getActivity().finish();
return;
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.GINGERBREAD) {
setCameraDisplayOrientation(camera);
}
else {
camera.setDisplayOrientation(90);
}
setCamera(camera);
setCameraZoomDisplay(camera);
if (getSurfaceHolder() != null) {
startPreview();
if (getMode() == MODE_VIDEO) {
initRecording();
}
}
}
private void setCameraDisplayOrientation(Camera camera) {
CameraInfo info = new CameraInfo();
Camera.getCameraInfo(0, info);
int rotation = getActivity().getWindowManager().getDefaultDisplay().getRotation();
int degrees = 0;
switch (rotation) {
case Surface.ROTATION_0:
degrees = 0;
break;
case Surface.ROTATION_90:
degrees = 90;
break;
case Surface.ROTATION_180:
degrees = 180;
break;
case Surface.ROTATION_270:
degrees = 270;
break;
}
int result = (info.orientation - degrees + 360) % 360;
camera.setDisplayOrientation(result);
}
private void initRecording() {
MediaRecorder recorder = new MediaRecorder();
setRecorder(recorder);
Camera camera = getCamera();
camera.unlock();
recorder.setCamera(camera);
recorder.setAudioSource(MediaRecorder.AudioSource.MIC);
recorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
CamcorderProfile cp = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cp);
String extension;
switch (cp.fileFormat) {
case MediaRecorder.OutputFormat.MPEG_4:
extension = "mp4";
break;
case MediaRecorder.OutputFormat.THREE_GPP:
extension = "3gp";
break;
default:
extension = "tmp";
}
setVideoMimeType(MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension));
File toFile = new File(getActivity().getCacheDir(), "tempvideo.tmp");
if (toFile.exists()) {
toFile.delete();
}
setTempFile(toFile);
recorder.setOutputFile(toFile.getPath());
recorder.setPreviewDisplay(getSurfaceHolder().getSurface());
try {
recorder.prepare();
setRecorderInitialized(true);
}
catch (IllegalStateException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
private boolean startRecording() {
try {
getRecorder().start();
setRecording(true);
ImageView actionImageView = getActionImageView();
actionImageView.setImageResource(R.drawable.record_red);
}
catch (Exception e) {
getCamera().lock();
}
return true;
}
private void stopRecording() {
MediaRecorder recorder = getRecorder();
if (recorder != null && isRecording()) {
recorder.stop();
setRecording(false);
setRecorderInitialized(false);
try {
insertVideo();
}
catch (IOException e) {
e.printStackTrace();
}
initRecording();
ImageView actionImageView = getActionImageView();
actionImageView.setImageResource(R.drawable.record_green);
}
}
private void destroyRecorder() {
MediaRecorder recorder = getRecorder();
recorder.release();
setRecorder(null);
getCamera().lock();
}
你有沒有想過是什麼導致閃爍和調整大小?我正在處理與我正在進行的項目相同的問題。 – MattDavis
@MattDavis抱歉,遺憾的是沒有。 –