How can I resolve rotating 90 degree of video when playing on android?
android, surfaceview, video
Solution
Preview orientation depends on the orientation of the device and camera orientation.
Basically what you need is to calculate the orientation of the camera preview based on those conditions.
We need two help methods:
1 - Calculate the device orientation:
public int getDeviceOrientation(Context context) {
int degrees = 0;
WindowManager windowManager =
(WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
int rotation = windowManager.getDefaultDisplay().getRotation();
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;
}
return degrees;
}
2 - Calculate Camera preview rotation:
public static int getPreviewOrientation(Context context, int cameraId) {
int temp = 0;
int previewOrientation = 0;
Camera.CameraInfo cameraInfo = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, cameraInfo);
int deviceOrientation = getDeviceOrientation(context);
temp = cameraInfo.orientation - deviceOrientation + 360;
previewOrientation = temp % 360;
return previewOrientation;
}
At your code, before mediaRecorder.prepare();
int rotation = getPreviewOrientation(context, cameraId);
mediaRecorder.setOrientationHint(rotation);
To use those methods is required a context and the camera Id in use.
Get the camera Id for back camera:
public int getCamaraBackId(){
numberOfCameras = Camera.getNumberOfCameras();
CameraInfo cameraInfo = new CameraInfo();
for (int i = 0; i < numberOfCameras; i++) {
Camera.getCameraInfo(i, cameraInfo);
if (cameraInfo.facing == CameraInfo.CAMERA_FACING_BACK) {
return i;
}
}
return -1 // Device do not have back camera !!!!???
}
Open camera with `Camera.open(getCamaraBackId());`
For more information see Android documentation
Problem
I have a video recorder on my android app which works well. Most important piece of code is here: ``` protected void startRecording() throws IOException { mCamera.stopPreview(); mCamera.unlock(); mrec = new MediaRecorder(); mrec.setCamera(mCamera); mrec.setAudioSource(MediaRecorder.AudioSource.MIC); mrec.setVideoSource(MediaRecorder.VideoSource.CAMERA); mrec.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mrec.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB); mrec.setVideoEncoder(MediaRecorder.VideoEncoder.MPEG_4_SP); mrec.setOutputFile(Videopath); mrec.setPreviewDisplay(surfaceHolder.getSurface()); mrec.prepare(); isRecording=true; mrec.start(); } //------------------------------------------------------------- protected void stopRecording() { releaseOnExit(); mCamera = Camera.open(); mCamera.lock(); surfaceView = (SurfaceView) findViewById(R.id.surface_camera); surfaceHolder = surfaceView.getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); Parameters params = mCamera.getParameters(); mCamera.setParameters(params); mCamera.setDisplayOrientation(90); try { mCamera .setPreviewDisplay(surfaceHolder); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } mCamera .startPreview(); btnlistToggle.setEnabled(true); } //------------------------------------------------------------- @Override public void surfaceCreated(SurfaceHolder holder) { if (mCamera != null) { Parameters params = mCamera.getParameters(); mCamera.setParameters(params); mCamera.setDisplayOrientation(90); try { mCamera .setPreviewDisplay(holder); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } mCamera .startPreview(); } else { Toast.makeText(getApplicationContext(), "Camera not available!", Toast.LENGTH_LONG).show(); VideoRecorderActivity.this.finish(); } } ``` I have a video player on my app ,too. But when I play my recorded videos , video 90 degree rotates which isn't good for showing. I play my recorded video by KMplayer on windows But video has 90 degree rotation at there . And I must tell that I use `SurfaceView` for player. How can I resolve it? It is necessary for me or for other users read this post.