我正在我的应用程序中集成照片/视频捕获,并且在视频捕获方面遇到问题。每当视频录制开始时,屏幕就会闪烁,我会短暂停顿,然后开始视频捕获。但是,使用手机的相机应用程序,根本没有闪烁/暂停。
此外,我的相机预览显示会在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();
}