I'm using Android native camera to record videos from my app, I'm using the class android.hardware.Camera
for that.
My application is all portrait. My issue is when start actual recording mMediaRecorder.start()
the camera gets rotated to be in landscape while recording and gives a weird preview:
After adding mCameraPreview.setRotation(90)
My camera preview is working fine but the preview while recording still portrait:
The weird thing is after recording when viewing or displaying the video, it is in the portrait mode, and everything is OK with the video itself:
So, the problem is only in the camera view while recording the video, which I'm trying to fix with all of the available possibilities but got nothing, there are dozens of such issues on SO but I no one of them is solving my problem, I tried this & this & this but nothing is working for me.
I'm going like the following:
in 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)
in 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();
}
}