Android:视频录制输出方向翻转

Posted

技术标签:

【中文标题】Android:视频录制输出方向翻转【英文标题】:Android: Video recording output orientation flipped 【发布时间】:2016-02-14 13:31:03 【问题描述】:

我开发了一个可以录制视频的应用。录制开始时,视频以旋转方向录制。录制时的视频视图向左翻转。我不明白为什么会这样。有人可以帮我解决这个问题。我的编码如下:

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_
android:layout_>

<SurfaceView android:id="@+id/videoview"
    android:layout_
    android:layout_>

    </SurfaceView>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_
    android:layout_
    android:orientation="vertical">

</LinearLayout>

<Button
    android:text="Rec"
    android:id="@+id/mybutton"
    android:layout_
    android:layout_
    android:layout_gravity="center_horizontal|bottom">

    </Button>
</FrameLayout>

logcat 没有错误。任何建议/帮助将不胜感激。谢谢。

【问题讨论】:

【参考方案1】:

输出取决于setOrientationHint(..)

使用下面设置orientationHint

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`

您需要enable() 定向监听器并禁用onDestroy()

第1步:声明veriable和Object:

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

第二步:添加这个方法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();
        

    

还有enable()onResume()

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

    

第3步:使用MediaRecorder准备后禁用

myOrientationEventListener.disable();

【讨论】:

嗨@amit,您能否提供完整的代码。我在我的应用程序中面临同样的问题。您的代码似乎解决了这个问题

以上是关于Android:视频录制输出方向翻转的主要内容,如果未能解决你的问题,请参考以下文章

Android:如何旋转或更改使用库录制的视频的方向?

在录制视频时处理相机翻转而不停止它反应原生

Android翻盖前置摄像头镜头翻转视频

android开发设置屏蔽录制

Android多媒体功能开发(13)——使用MediaRecorder类录制视频

Android 录制没有音频的视频