Android视频播放器实现小窗口和全屏状态切换:
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