Android: video output orientation

I developed an application that can record video. When recording starts, the video is recorded in a rotated orientation. The video image is turned left during recording. I can’t understand why this is so. Can someone help me on this. My encodings are as follows:

MainActivity.java:

import android.app.Activity; import android.media.CamcorderProfile; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.Button; import java.io.File; import java.io.IOException; import java.text.SimpleDateFormat; public class MainActivity extends Activity implements SurfaceHolder.Callback{ Button myButton; MediaRecorder mediaRecorder; SurfaceHolder surfaceHolder; boolean recording; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); recording = false; mediaRecorder = new MediaRecorder(); initMediaRecorder(); setContentView(R.layout.activity_main); SurfaceView myVideoView = (SurfaceView)findViewById(R.id.videoview); surfaceHolder = myVideoView.getHolder(); surfaceHolder.addCallback(this); surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); myButton = (Button)findViewById(R.id.mybutton); myButton.setOnClickListener(myButtonOnClickListener); } private Button.OnClickListener myButtonOnClickListener = new Button.OnClickListener(){ @Override public void onClick(View arg0) { // TODO Auto-generated method stub if(recording){ mediaRecorder.stop(); mediaRecorder.release(); finish(); }else{ mediaRecorder.start(); recording = true; myButton.setText("STOP"); } }}; @Override public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) { // TODO Auto-generated method stub } @Override public void surfaceCreated(SurfaceHolder arg0) { // TODO Auto-generated method stub prepareMediaRecorder(); } @Override public void surfaceDestroyed(SurfaceHolder arg0) { // TODO Auto-generated method stub } private void initMediaRecorder(){ java.util.Date date= new java.util.Date(); String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss") .format(date.getTime()); String path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyFolder/"; File dir = new File(path); if(!dir.exists()) dir.mkdirs(); String myfile = new String(path + File.separator + "VID_"+ timeStamp + ".mp4"); mediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); mediaRecorder.setOrientationHint(0); CamcorderProfile camcorderProfile_HQ = CamcorderProfile.get(CamcorderProfile.QUALITY_720P); mediaRecorder.setProfile(camcorderProfile_HQ); mediaRecorder.setOutputFile(myfile); mediaRecorder.setMaxDuration(60000); // Set max duration 60 sec. mediaRecorder.setMaxFileSize(5000000); // Set max file size 5M } private void prepareMediaRecorder(){ mediaRecorder.setPreviewDisplay(surfaceHolder.getSurface()); try { mediaRecorder.prepare(); } catch (IllegalStateException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } 

activity_main.xml:

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <SurfaceView android:id="@+id/videoview" android:layout_width="wrap_content" android:layout_height="wrap_content"> </SurfaceView> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> </LinearLayout> <Button android:text="Rec" android:id="@+id/mybutton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center_horizontal|bottom"> </Button> </FrameLayout> 

There is no error in logcat. Any suggestions / help would be greatly appreciated. Thanks.

0
android video-capture android-camera surfaceview android-mediarecorder
source share
1 answer

The output depends on setOrientationHint(..)

set orientationClick here

 mediaRecorder.setOrientationHint(mOrientation); if (Build.MODEL.equalsIgnoreCase("Nexus 6") && flag == 1) { if (mOrientation == 90) { mediaRecorder.setOrientationHint(mOrientation); } else if (mOrientation == 180) { mediaRecorder.setOrientationHint(0); } else { mediaRecorder.setOrientationHint(180); } } else if (mOrientation == 90 && flag == 1) { mediaRecorder.setOrientationHint(270); } else if (flag == 1) { mediaRecorder.setOrientationHint(mOrientation); } mOrientation you can get from `OrientationEventListener` 

You need to enable() orientation listener and disable onDestroy()

Step 1: declare believable and Object:

  OrientationEventListener myOrientationEventListener; int iOrientation = 0; int mOrientation = 90; 

Step 2: add this onCreate() method

 private void identifyOrientationEvents() { myOrientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) { @Override public void onOrientationChanged(int iAngle) { final int iLookup[] = {0, 0, 0, 90, 90, 90, 90, 90, 90, 180, 180, 180, 180, 180, 180, 270, 270, 270, 270, 270, 270, 0, 0, 0}; // 15-degree increments if (iAngle != ORIENTATION_UNKNOWN) { int iNewOrientation = iLookup[iAngle / 15]; if (iOrientation != iNewOrientation) { iOrientation = iNewOrientation; if (iOrientation == 0) { mOrientation = 90; } else if (iOrientation == 270) { mOrientation = 0; } else if (iOrientation == 90) { mOrientation = 180; } } mPhotoAngle = normalize(iAngle); } } }; if (myOrientationEventListener.canDetectOrientation()) { myOrientationEventListener.enable(); } } 

also enable() in onResume()

  @Override protected void onResume() { super.onResume(); try { if (myOrientationEventListener != null) myOrientationEventListener.enable(); } catch (Exception e1) { e1.printStackTrace(); } } 

Step 3: Disable After Preparing MediaRecorder Using

 myOrientationEventListener.disable(); 
0
source share

All Articles