Skip to content

悬浮窗模式

ZERO edited this page Apr 5, 2019 · 2 revisions

功能介绍

  • 超级播放器目前支持悬浮窗模式播放

使用方法

  • SuperPlayerGlobalConfig配置好悬浮窗的相关参数
// 设置悬浮窗的初始位置和宽高
SuperPlayerGlobalConfig.TXRect rect = new SuperPlayerGlobalConfig.TXRect();
rect.x = 0;
rect.y = 0;
rect.width = 810;
rect.height = 540;
config.floatViewRect = rect;
  • 调用方法开启悬浮窗
mSuperPlayerView.requestPlayMode(SuperPlayerConst.PLAYMODE_FLOAT);

技术揭秘

SuperPlayerView 是如何实现悬浮播放的? 往 WindowManager 直接塞 View 进去

mWindowManager = (WindowManager) mContext.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
mWindowParams = new WindowManager.LayoutParams();
  mWindowParams.type = WindowManager.LayoutParams.TYPE_SYSTEM_ALERT;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
    mWindowParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
} else {
    mWindowParams.type = WindowManager.LayoutParams.TYPE_PHONE;
}
mWindowParams.flags = WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
mWindowParams.format = PixelFormat.TRANSLUCENT;
mWindowParams.gravity = Gravity.LEFT | Gravity.TOP;
SuperPlayerGlobalConfig.TXRect rect = prefs.floatViewRect;
mWindowParams.x = rect.x;
mWindowParams.y = rect.y;
mWindowParams.width = rect.width;
mWindowParams.height = rect.height;
try {
    mWindowManager.addView(mVodControllerFloat, mWindowParams);
} catch (Exception e) {
    Toast.makeText(SuperPlayerView.this.getContext(), "悬浮播放失败", Toast.LENGTH_SHORT).show();
    return;
}
TXCloudVideoView videoView = mVodControllerFloat.getFloatVideoView();
if (videoView != null) {
    if (mCurrentPlayType == SuperPlayerConst.PLAYTYPE_VOD) {
        mVodPlayer.setPlayerView(videoView);
    } else {
        mLivePlayer.setPlayerView(videoView);
    }
    resume();
}

需要特别注意的是悬浮窗权限

Clone this wiki locally