Android: Video recording output orientation flipped

后端 未结 1 848
天命终不由人
天命终不由人 2020-12-07 06:16

I have developed an app that can record video. When the recording starts the video is recorded in a rotated orientation. The video view while recording is flipped to the lef

相关标签:
1条回答
  • 2020-12-07 06:45

    Output depend on setOrientationHint(..)

    set orientationHint using below

    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 veriable and Object:

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

    Step 2: add this method onCreate()

    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 讨论(0)
提交回复
热议问题