我正在使用Android原生相机记录我的应用中的视频,我正在使用课程android.hardware.Camera
。
我的申请全是肖像。我的问题是当开始实际录制mMediaRecorder.start()
时,相机会在录制时旋转到风景中并进行奇怪的预览:
添加mCameraPreview.setRotation(90)
后我的相机预览工作正常,但录制静态时预览:
奇怪的是,在观看或显示视频时录制后,它处于纵向模式,视频本身一切正常:
所以,问题只出在录制视频时的摄像机视图中,我正试图用所有可能的可能性修复但没有得到任何东西,SO上有几十个这样的问题,但我没有一个是解决我的问题,我试过this& this& this但是没有什么对我有用。
我会像以下一样:
MyRecordingActivity.Java :mCamera = getCameraInstance(); // Camera instance
mCameraPreview = new CameraPreview(this, mCamera); // SurfaceView with SurfaceHolder.Callback
preview.addView(mCameraPreview); // the view that is used to contain the camera preview (it is a FrameLayout)
CameraPreview.java SurfaceView
:
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
try {
Camera.Parameters parameters = mCamera.getParameters();
parameters.set("orientation", "portrait");
parameters.set("rotation",90);
mCamera.setParameters(parameters);
mCamera.setDisplayOrientation(90);
openCamera(); // a HandlerThread to open the camera
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
// to initialize & prepare the MediaRecorder
initRecorder();
prepareRecorder();
} catch (IOException e) {}
}
/**
* Initializing the video recorder
*/
public void initRecorder() {
recorder = new MediaRecorder();
recorder.setAudioSource(MediaRecorder.AudioSource.DEFAULT);
recorder.setVideoSource(MediaRecorder.VideoSource.DEFAULT);
CamcorderProfile cpHigh = CamcorderProfile
.get(CamcorderProfile.QUALITY_HIGH);
recorder.setProfile(cpHigh);
recorder.setOutputFile(getOutputMediaFile(false).getAbsolutePath());
recorder.setMaxDuration(20000); // 20 seconds
}
/**
* Preparing the video recorder
*/
public void prepareRecorder() {
recorder.setPreviewDisplay(mCameraPreview.mSurfaceHolder.getSurface());
recorder.setOrientationHint(90);
try {
recorder.prepare();
} catch (IllegalStateException e) {
e.printStackTrace();
finish();
} catch (IOException e) {
e.printStackTrace();
finish();
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int format,
int width, int height) {
// start preview with new settings
try {
mCamera.setDisplayOrientation(90);
mCamera.setPreviewDisplay(surfaceHolder);
mCamera.startPreview();
//fixVideoPreview();
} catch (Exception e) {
// intentionally left blank for a test
}
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
try {
mCamera.stopPreview();
mCamera.release();
} catch (Exception ex) {
ex.printStackTrace();
}
try {
if (recording) {
recorder.stop();
recording = false;
}
recorder.release();
} catch (Exception ex) {
ex.printStackTrace();
}
}