使用全屏沉浸模式实现播放器的横竖屏切换

时间:2024-04-07 13:32:14
研究了下横竖屏切换,感觉使用沉浸模式效果还可以。
前些时候分享的XLPlayer播放器只是横屏的,就给它加入竖屏功能。
首先改AndroidManifest.xml
<activity
    android:name=".SinglePlayerActivity"
    android:configChanges="orientation|screenSize"
    android:screenOrientation="portrait"
    android:theme="@style/AppNoActionBar"/>
设置自己的style,去掉ActionBar
styles.xml
<style name="AppNoActionBar" parent="Theme.AppCompat.Light.DarkActionBar">
           
    <item name="colorPrimary">@color/colorPrimary</item>               
    <item name="colorPrimaryDark">@color/colorPrimaryDark</item>               
    <item name="colorAccent">@color/colorAccent</item>
           
    <item name="windowActionBar">false</item>               
    <item name="windowNoTitle">true</item>
       
</style>
更改布局,变为不是全屏
使用全屏沉浸模式实现播放器的横竖屏切换
activity_single.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#000000">

    <RelativeLayout
        android:id="@+id/main_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <RelativeLayout
            android:id="@+id/portrait_player_view"
            android:layout_width="match_parent"
            android:layout_height="400dp">

            <TextureView
                android:id="@+id/m_xlsurface"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_alignParentStart="true"
                android:layout_alignParentTop="true" />

            <RelativeLayout
                android:id="@+id/player_view"
                android:layout_width="match_parent"
                android:layout_height="match_parent">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentBottom="true"
                    android:orientation="vertical">


                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="10dp">

                        <TextView
                            android:id="@+id/currentTime"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true"
                            android:text="0:0" />

                        <TextView
                            android:id="@+id/duration"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:text="0:0" />

                        <SeekBar
                            android:id="@+id/video_progress"
                            style="@style/Base.Widget.AppCompat.SeekBar"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_toLeftOf="@+id/duration"
                            android:layout_toRightOf="@id/currentTime"
                            android:background="@android:color/transparent" />
                    </RelativeLayout>

                    <RelativeLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="10dp"
                        android:layout_marginLeft="10dp">

                        <LinearLayout
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentLeft="true">

                            <TextView
                                android:id="@+id/seek_back"
                                android:layout_width="30dp"
                                android:layout_height="30dp"
                                android:background="@drawable/seek_back" />

                            <TextView
                                android:id="@+id/start"
                                android:layout_width="30dp"
                                android:layout_height="30dp"
                                android:layout_marginLeft="10dp"
                                android:background="@drawable/play" />

                            <TextView
                                android:id="@+id/seek_front"
                                android:layout_width="30dp"
                                android:layout_height="30dp"
                                android:layout_marginLeft="10dp"
                                android:background="@drawable/seek_front" />

                            <TextView
                                android:id="@+id/stop"
                                android:layout_width="30dp"
                                android:layout_height="30dp"
                                android:layout_marginLeft="10dp"
                                android:background="@drawable/stop" />

                            <TextView
                                android:id="@+id/setting"
                                android:layout_width="30dp"
                                android:layout_height="30dp"
                                android:layout_alignParentRight="true"
                                android:layout_marginLeft="10dp"
                                android:background="@drawable/setting" />

                            <TextView
                                android:layout_width="30dp"
                                android:layout_height="30dp"
                                android:layout_alignParentRight="true"
                                android:layout_marginLeft="10dp"
                                android:background="@drawable/setting"
                                android:onClick="orientationChange"/>

                        </LinearLayout>

                        <TextView
                            android:id="@+id/enable_tracker"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_alignParentRight="true"
                            android:layout_marginRight="10dp"
                            android:background="@drawable/ic_vr_tuoluo" />
                    </RelativeLayout>

                </LinearLayout>

                <LinearLayout
                    android:id="@+id/choose_vr_mode_layout"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_centerInParent="true"
                    android:background="#30ffffff"
                    android:orientation="vertical"
                    android:padding="15dp"
                    android:visibility="invisible"
                    tools:visibility="visible">

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:orientation="horizontal">

                        <TextView
                            android:id="@+id/player_nomal"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:background="@drawable/ic_nomal_play" />

                        <TextView
                            android:id="@+id/vr_model_ball"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_marginLeft="15dp"
                            android:background="@drawable/ic_vr_ball" />

                        <TextView
                            android:id="@+id/vr_model_two_eye"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_marginLeft="15dp"
                            android:background="@drawable/ic_vr_two_eye" />

                        <TextView
                            android:id="@+id/vr_model_building"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_marginLeft="15dp"
                            android:background="@drawable/ic_vr_building" />

                        <TextView
                            android:id="@+id/vr_model_little_star"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_marginLeft="15dp"
                            android:background="@drawable/ic_vr_little_star" />

                        <TextView
                            android:id="@+id/vr_model_pic"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_marginLeft="15dp"
                            android:background="@drawable/ic_vr_pic" />
                    </LinearLayout>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginTop="10dp"
                        android:background="#666666" />

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp">

                        <TextView
                            android:id="@+id/clockwise"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:background="@drawable/turn_left" />

                        <TextView
                            android:id="@+id/anti_clockwise"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_marginLeft="10dp"
                            android:background="@drawable/turn_right" />


                    </LinearLayout>

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="1dp"
                        android:layout_marginTop="10dp"
                        android:background="#666666" />

                    <LinearLayout
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="10dp"
                        android:gravity="center_vertical">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:text="后台播放"
                            android:textColor="#333333" />

                        <CheckBox
                            android:id="@+id/back_play"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:checked="false" />

                        <TextView
                            android:id="@+id/rate_plus"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:layout_marginLeft="10dp"
                            android:background="@drawable/plus" />

                        <TextView
                            android:id="@+id/now_rate"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_margin="10dp"
                            android:text="1.0" />

                        <TextView
                            android:id="@+id/rate_minus"
                            android:layout_width="30dp"
                            android:layout_height="30dp"
                            android:background="@drawable/minus" />
                    </LinearLayout>
                </LinearLayout>
            </RelativeLayout>

        </RelativeLayout>
    </RelativeLayout>

    <RelativeLayout
        android:id="@+id/landscape_player_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:onClick="notClick"
        android:visibility="invisible">

    </RelativeLayout>


</RelativeLayout>
放大缩小的图标就用设置的图标
SurfaceView改用TextureView,SurfaceView在切换横竖屏时会出现一小段黑屏
SinglePlayerActivity.java
public class SinglePlayerActivity extends Activity implements View.OnClickListener {
    private TextureView mTextureView; //SurfaceView改用TextureView,SurfaceView在切换横竖屏时会出现一小段黑屏
    private XLPlayer xlPlayer;
    SeekBar videoSeekBar;
    int videoProNow;
    TextView playIcon;
    TextView rateView;
    GestureDetector mGestureDetector;
    ScaleGestureDetector mScaleGestureDetector;
    float nowScale = 1f;//缩放的取值范围,0.5f 到 2f
    String url;
    boolean isBackPlay = false;
    MODEL_TYPE modelType = MODEL_TYPE.Rect;
    @SuppressLint("HandlerLeak")
    Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 1:
                    videoProNow = (int) xlPlayer.getVideoCurrentTime();
                    ((TextView) findViewById(R.id.currentTime)).setText(formatTime(videoProNow));
                    videoSeekBar.setProgress(videoProNow);
                    handler.sendEmptyMessageDelayed(1, 1000);
                    Statistics st = xlPlayer.getStatistics();
//                    System.out.println("fps = " + st.getFps() + " , bps = " + st.getFormatBps() + ", buffer = " + st.getBufferLength() + "ms");
                    break;
            }
        }
    };

    //orientation
    //屏幕状态
    enum ScreenState {
        SCREEN_FULL,
        SCREEN_SHRINK,
        SCREEN_PORTRAIT,
        SCREEN_LANDSCAPE,
        SCREEN_REVERSE_LANDSCAPE
    }
    private RelativeLayout landscapeView;//横屏view
    private RelativeLayout portraitView;//竖屏view
    private RelativeLayout playerView;//播放操作view

    private ScreenState screenState = ScreenState.SCREEN_PORTRAIT;//初始化屏幕状态

    private OrientationEventListener orientationEventListener;//监听手机方向
    private int rot = 0;//当前手机方向

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_single);
        url = getIntent().getStringExtra("media_url");
        mGestureDetector = new GestureDetector(this, simpleOnGestureListener);
        mScaleGestureDetector = new ScaleGestureDetector(this, scaleGestureListener);
        mTextureView = findViewById(R.id.m_xlsurface);
        mTextureView.setOnTouchListener(playerViewTouchListener);
        mTextureView.setKeepScreenOn(true);
        //监听TextureView的生命周期
        mTextureView.setSurfaceTextureListener(new TextureView.SurfaceTextureListener() {
            @Override
            public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
                Surface videoSurface = new Surface(surface);
                xlPlayer.setSurface(videoSurface);
            }

            @Override
            public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
                xlPlayer.resize(width, height);
            }

            @Override
            public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
                return false;
            }

            @Override
            public void onSurfaceTextureUpdated(SurfaceTexture surface) {

            }
        });
        videoSeekBar = (SeekBar) findViewById(R.id.video_progress);
        videoSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {

            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                handler.removeMessages(1);
            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                xlPlayer.seekTo(seekBar.getProgress());
                handler.sendEmptyMessage(1);
            }
        });
        playIcon = (TextView) findViewById(R.id.start);
        playIcon.setOnClickListener(playListener);
        rateView = (TextView) findViewById(R.id.now_rate);
        findViewById(R.id.seek_front).setOnClickListener(this);
        findViewById(R.id.seek_back).setOnClickListener(this);
        findViewById(R.id.stop).setOnClickListener(this);
        findViewById(R.id.clockwise).setOnClickListener(this);
        findViewById(R.id.anti_clockwise).setOnClickListener(this);
        findViewById(R.id.rate_plus).setOnClickListener(this);
        findViewById(R.id.rate_minus).setOnClickListener(this);
        findViewById(R.id.setting).setOnClickListener(this);
        findViewById(R.id.player_nomal).setOnClickListener(this);
        findViewById(R.id.vr_model_ball).setOnClickListener(this);
        findViewById(R.id.vr_model_building).setOnClickListener(this);
        findViewById(R.id.vr_model_little_star).setOnClickListener(this);
        findViewById(R.id.vr_model_two_eye).setOnClickListener(this);
        findViewById(R.id.vr_model_pic).setOnClickListener(this);
        findViewById(R.id.enable_tracker).setOnClickListener(this);
        ((CheckBox) findViewById(R.id.back_play)).setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                isBackPlay = isChecked;
            }
        });
        xlPlayer = new XLPlayer(this);
        xlPlayer.setForceSwDecode(MainActivity.forceSwDecode);
        xlPlayer.setOnPlayerStatusChangeListener(playerStatusChangeListener);
        xlPlayer.setOnErrorCodeListener(new OnErrorCodeListener() {
            @Override
            public void onGetErrorCode(int errorCode) {
                switch (errorCode) {
                    case 100:
                        Toast.makeText(SinglePlayerActivity.this, "cound not open url", Toast.LENGTH_SHORT).show();
                        break;
                    default:
                        Toast.makeText(SinglePlayerActivity.this, "has error ,code is " + errorCode, Toast.LENGTH_SHORT).show();
                        break;
                }
            }
        });
        if (getIntent().hasExtra("model")){
            modelType = (MODEL_TYPE) getIntent().getSerializableExtra("model");
        }

//        xlPlayer.playVideo(url, modelType);
        if (modelType == MODEL_TYPE.VR || modelType == MODEL_TYPE.Ball || modelType == MODEL_TYPE.Architecture) {
            xlPlayer.setEnableTracker(true);
        }


        //使用这个方案,会出现view掉到状态栏后边
        View decorView = getWindow().getDecorView();
        int mShowFlags =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        decorView.setSystemUiVisibility(mShowFlags);
        //计算状态栏高度
        int statusBarHeight = -1;
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            statusBarHeight = getResources().getDimensionPixelSize(resourceId);
        }
        //调整竖屏时的主view的位置
        RelativeLayout mainView = findViewById(R.id.main_view);
        RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) mainView.getLayoutParams();
        layoutParams.topMargin =statusBarHeight;



        landscapeView = findViewById(R.id.landscape_player_view);//横屏view
        portraitView = findViewById(R.id.portrait_player_view);//竖屏view
        playerView = findViewById(R.id.player_view);//播放器的各种按钮view
        // 手机方向监听器
        orientationEventListener = new OrientationEventListener(this, SensorManager.SENSOR_DELAY_NORMAL) {
            @Override
            public void onOrientationChanged(int orientation) {
                if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) {
                    return;
                }
                if (orientation > 350 || orientation < 10) { //0度
                    orientation = 0;
                } else if (orientation > 80 && orientation < 100) { //90度
                    orientation = 90;
                } else if (orientation > 170 && orientation < 190) { //180度
                    orientation = 180;
                } else if (orientation > 260 && orientation < 280) { //270度
                    orientation = 270;
                } else {
                    return;
                }
                if (rot != orientation) {
                    rot = orientation;
                    switch (orientation) {
                        case 0:
                            if (screenState == ScreenState.SCREEN_SHRINK) {
                                screenState = ScreenState.SCREEN_PORTRAIT;
                                return;
                            }

                            landscapeView.removeView(mTextureView);
                            portraitView.addView(mTextureView);

                            landscapeView.removeView(playerView);
                            portraitView.addView(playerView);
                            shrinkScreen();
                            portraitView.setVisibility(View.VISIBLE);
                            landscapeView.setVisibility(View.INVISIBLE);

                            screenState = ScreenState.SCREEN_PORTRAIT;
                            break;
                        case 90:
                            if (screenState == ScreenState.SCREEN_FULL) {
                                screenState = ScreenState.SCREEN_LANDSCAPE;
                                return;
                            }
                            if (screenState == ScreenState.SCREEN_PORTRAIT || screenState == ScreenState.SCREEN_SHRINK) {
                                portraitView.removeView(mTextureView);
                                landscapeView.addView(mTextureView);

                                portraitView.removeView(playerView);
                                landscapeView.addView(playerView);

                                landscapeView.setVisibility(View.VISIBLE);
                                portraitView.setVisibility(View.INVISIBLE);

                                View decorView = getWindow().getDecorView();
                                int mHideFlags =
                                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                                | View.SYSTEM_UI_FLAG_FULLSCREEN
                                                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
                                decorView.setSystemUiVisibility(mHideFlags);
                            }
                            if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
                                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
                            }
                            screenState = ScreenState.SCREEN_LANDSCAPE;
                            break;
                        case 270:
                            if (screenState == ScreenState.SCREEN_PORTRAIT || screenState == ScreenState.SCREEN_SHRINK) {
                                portraitView.removeView(mTextureView);
                                landscapeView.addView(mTextureView);

                                portraitView.removeView(playerView);
                                landscapeView.addView(playerView);

                                landscapeView.setVisibility(View.VISIBLE);
                                portraitView.setVisibility(View.INVISIBLE);

                                View decorView = getWindow().getDecorView();
                                int mHideFlags =
                                        View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                                                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                                                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                                                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                                                | View.SYSTEM_UI_FLAG_FULLSCREEN
                                                | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
                                decorView.setSystemUiVisibility(mHideFlags);
                            }
                            if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
                                setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
                            }
                            screenState = ScreenState.SCREEN_REVERSE_LANDSCAPE;
                            break;
                    }
                }
            }
        };
        if (orientationEventListener.canDetectOrientation()) {
            orientationEventListener.enable();
        } else {
            orientationEventListener.disable();
        }

    }

    View.OnTouchListener playerViewTouchListener = new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            mScaleGestureDetector.onTouchEvent(event);
            mGestureDetector.onTouchEvent(event);
            return true;
        }
    };

    ScaleGestureDetector.SimpleOnScaleGestureListener scaleGestureListener = new ScaleGestureDetector.SimpleOnScaleGestureListener() {
        @Override
        public boolean onScale(ScaleGestureDetector detector) {
            nowScale *= detector.getScaleFactor();
            nowScale = nowScale > 2f ? 2f : (nowScale < 0.5f ? 0.5f : nowScale);
            xlPlayer.setScale(nowScale);
            return true;
        }
    };
    float startx, starty;
    GestureDetector.SimpleOnGestureListener simpleOnGestureListener = new GestureDetector.SimpleOnGestureListener() {
        @Override
        public boolean onDown(MotionEvent e) {
            startx = e.getX();
            starty = e.getY();
            return super.onDown(e);
        }

        @Override
        public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
            if (xlPlayer.getModelType() == MODEL_TYPE.Planet) {
                float centerX = mTextureView.getWidth() / 2f;
                float centerY = mTextureView.getHeight() / 2f;
                float startAngle = (float) Math.atan2(starty - centerY, startx - centerX);
                float endAngle = (float) Math.atan2(e2.getY() - centerY, e2.getX() - centerX);
                xlPlayer.setRotation(0, 0, startAngle - endAngle);
                startx = e2.getX();
                starty = e2.getY();
            } else {
                xlPlayer.setRotation((float) (distanceY / 10f / 180f * Math.PI), (float) (distanceX / 10f / 180f * Math.PI), 0);
            }

            return true;
        }
    };


    View.OnClickListener playListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            xlPlayer.playVideo(url, 0, modelType);
        }
    };

    OnPlayerStatusChangeListener playerStatusChangeListener = new OnPlayerStatusChangeListener() {
        @Override
        public void onStart() {
            int videoProMax = (int) xlPlayer.getVideoTotalTime();
            ((TextView) findViewById(R.id.duration)).setText(formatTime(videoProMax));
            videoSeekBar.setMax(videoProMax);
            handler.sendEmptyMessageDelayed(1, 1000);
            playIcon.setBackground(getResources().getDrawable(R.drawable.pause));
            playIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    xlPlayer.pauseVideo();
                }
            });
        }

        @Override
        public void onPause() {
            super.onPause();
            handler.removeCallbacksAndMessages(null);
            playIcon.setBackground(getResources().getDrawable(R.drawable.play));
            handler.sendEmptyMessageDelayed(1, 1000);
            playIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    xlPlayer.resumeVideo();
                }
            });
        }

        @Override
        public void onResume() {
            super.onResume();
            handler.sendEmptyMessageDelayed(1, 1000);
            playIcon.setBackground(getResources().getDrawable(R.drawable.pause));
            playIcon.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    xlPlayer.pauseVideo();
                }
            });
        }

        @Override
        public void onStop() {
            super.onStop();
            if (handler != null) {
                videoSeekBar.setProgress(0);
                ((TextView) findViewById(R.id.currentTime)).setText(formatTime(0));
                ((TextView) findViewById(R.id.duration)).setText(formatTime(0));
                handler.removeCallbacksAndMessages(null);
                playIcon.setBackground(getResources().getDrawable(R.drawable.play));
                playIcon.setOnClickListener(playListener);
            }
        }

        @Override
        public void onPrepared() {
            super.onPrepared();
            playIcon.setOnClickListener(playListener);
        }
    };


    @Override
    protected void onResume() {
        super.onResume();
        if (isBackPlay) {
            xlPlayer.setPlayBackground(false);
        } else {
            xlPlayer.onResume();
        }

    }

    @Override
    protected void onPause() {
        super.onPause();
        if (isBackPlay) {
            xlPlayer.setPlayBackground(true);
        } else {
            xlPlayer.onPause();
        }

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        orientationEventListener.disable();
        handler.removeCallbacksAndMessages(null);
        handler = null;
        xlPlayer.releasePlayer();
        mTextureView = null;
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    String formatTime(int time) {
        int h = time / 60 / 60;
        int m = (time - h * 3600) / 60;
        int s = time - h * 3600 - m * 60;
        return h + ":" + m + ":" + s;
    }

    @Override
    public void onClick(View v) {
        float nowRate;
        switch (v.getId()) {
            case R.id.seek_front:
                xlPlayer.seekTime(10);
                break;
            case R.id.seek_back:
                xlPlayer.seekTime(-10);
                break;
            case R.id.stop:
                xlPlayer.stopVideo();
                break;
            case R.id.clockwise:
                xlPlayer.rotate(false);
                break;
            case R.id.anti_clockwise:
                xlPlayer.rotate(true);
                break;
            case R.id.rate_plus:
                nowRate = Float.parseFloat(rateView.getText().toString());
                if (nowRate < 2.0) {
                    nowRate += 0.1f;
                    xlPlayer.setRate(nowRate);
                    rateView.setText(format("%.1f", nowRate));
                }
                break;
            case R.id.rate_minus:
                nowRate = Float.parseFloat(rateView.getText().toString());
                if (nowRate > 0.5) {
                    nowRate -= 0.1f;
                    xlPlayer.setRate(nowRate);
                    rateView.setText(format("%.1f", nowRate));
                }
                break;
            case R.id.setting:
                findViewById(R.id.choose_vr_mode_layout).setVisibility(findViewById(R.id.choose_vr_mode_layout).getVisibility() == View.INVISIBLE ? View.VISIBLE : View.INVISIBLE);
                break;
            case R.id.player_nomal:
                nowScale = 1f;
                xlPlayer.setEnableTracker(false);
                findViewById(R.id.choose_vr_mode_layout).setVisibility(View.INVISIBLE);
                xlPlayer.changeModel(MODEL_TYPE.Rect);
                break;
            case R.id.vr_model_ball:
                nowScale = 1f;
                xlPlayer.setEnableTracker(true);
                findViewById(R.id.choose_vr_mode_layout).setVisibility(View.INVISIBLE);
                xlPlayer.changeModel(MODEL_TYPE.Ball);
                break;
            case R.id.vr_model_building:
                nowScale = 1f;
                xlPlayer.setEnableTracker(false);
                findViewById(R.id.choose_vr_mode_layout).setVisibility(View.INVISIBLE);
                xlPlayer.changeModel(MODEL_TYPE.Architecture);
                break;
            case R.id.vr_model_little_star:
                nowScale = 1f;
                xlPlayer.setEnableTracker(false);
                findViewById(R.id.choose_vr_mode_layout).setVisibility(View.INVISIBLE);
                xlPlayer.changeModel(MODEL_TYPE.Planet);
                break;
            case R.id.vr_model_pic:
                nowScale = 1f;
                xlPlayer.setEnableTracker(false);
                findViewById(R.id.choose_vr_mode_layout).setVisibility(View.INVISIBLE);
                xlPlayer.changeModel(MODEL_TYPE.Expand);
                break;
            case R.id.vr_model_two_eye:
                nowScale = 1f;
                xlPlayer.setEnableTracker(true);
                findViewById(R.id.choose_vr_mode_layout).setVisibility(View.INVISIBLE);
                xlPlayer.changeModel(MODEL_TYPE.VR);
                break;
            case R.id.enable_tracker:
                xlPlayer.setEnableTracker(!xlPlayer.getEnableTracker());
                break;
        }
    }


    public void notClick(View v){

    }
    public void orientationChange(View v){
        if(screenState == ScreenState.SCREEN_PORTRAIT || screenState == ScreenState.SCREEN_SHRINK){
            portraitView.removeView(mTextureView);
            landscapeView.addView(mTextureView);

            portraitView.removeView(playerView);
            landscapeView.addView(playerView);
            fullScreen();
            landscapeView.setVisibility(View.VISIBLE);
            portraitView.setVisibility(View.INVISIBLE);
            screenState = ScreenState.SCREEN_FULL;
        }else if(screenState == ScreenState.SCREEN_FULL ||
                screenState == ScreenState.SCREEN_REVERSE_LANDSCAPE ||
                screenState == ScreenState.SCREEN_LANDSCAPE){
            landscapeView.removeView(mTextureView);
            portraitView.addView(mTextureView);

            landscapeView.removeView(playerView);
            portraitView.addView(playerView);
            shrinkScreen();
            portraitView.setVisibility(View.VISIBLE);
            landscapeView.setVisibility(View.INVISIBLE);
            screenState = ScreenState.SCREEN_SHRINK;
        }
    }

    //全屏
    private void fullScreen() {
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE);
        }
        View decorView = getWindow().getDecorView();
        int mHideFlags =
                View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
        decorView.setSystemUiVisibility(mHideFlags);
    }
    //退出全屏
    private void shrinkScreen() {
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        View decorView = getWindow().getDecorView();
        int mShowFlags =
                View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
                        | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                        | View.SYSTEM_UI_FLAG_LAYOUT_STABLE;
        decorView.setSystemUiVisibility(mShowFlags);
    }


}
使用全屏沉浸模式实现播放器的横竖屏切换
gif看起来会有卡顿,实际运行一点都不卡