Android播放器实现横竖屏切换

41 篇文章 0 订阅

Android视频播放器实现小窗口和全屏状态切换:

Demo地址 (gitee)

1、针对全屏按钮添加以下方法:

controller.setFullScreenListener(new FullScreenListener() {
   @Override
     public void onClick(View view) {
         // TODO: 2021/8/31 横竖屏切换
         Log.d("caowj", "横竖屏切换监听响应");
         int direction = getActivity(getContext()).getResources().getConfiguration().orientation;
         if (direction == Configuration.ORIENTATION_PORTRAIT) {
             Log.d("caowj", "当前竖屏");
             startFullScreen();
         } else if (direction == Configuration.ORIENTATION_LANDSCAPE) {
             Log.d("caowj", "当前横屏");
             exitFullScreen();
         } else {
             Log.d("caowj", "未处理的direction:" + direction);
         }
     }
 });
    public static Activity getActivity(Context context) {
        if (context == null) {
            return null;
        }
        if (context instanceof Activity) {
            return (Activity) context;
        } else if (context instanceof ContextWrapper) {
            return getActivity(((ContextWrapper) context).getBaseContext());
        }
        return null;
    }
    
    private void startFullScreen() {
        // 设置为横屏模式
        getActivity(getContext()).getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getActivity(getContext()).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

        // 切换全屏播放前当前VideoPlayerView的父容器
        ViewGroup viewGroup = (ViewGroup) this.getParent();
        viewGroup.removeView(this);
        LayoutParams lp = new LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
        lp.setMargins(0, 0, 0, 0);
        viewGroup.addView(this, lp);
    }

    private void exitFullScreen() {
        // 设置为竖屏模式
        getActivity(getContext()).getWindow().clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
        getActivity(getContext()).setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        // 切换全屏播放前当前VideoPlayerView的父容器
        ViewGroup viewGroup = (ViewGroup) this.getParent();
        viewGroup.removeView(this);
        LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        viewGroup.addView(this, lp);
    }

2、注意事项:

a、横竖屏切换方法需要放在自定义的播放器(PlayView)中,否则不生效;
b、Activity声明时添加以下配置,屏幕切换时不会重新执行onCreate方法:

android:configChanges=“orientation|keyboardHidden|screenSize”

参考:https://blog.csdn.net/u010072711/article/details/51517170

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值