Skip to content

Commit 47f72ea

Browse files
committed
fix: 🐛 (xgplayer) enhance touch event handling, fixed #1836
1 parent bc342ef commit 47f72ea

File tree

2 files changed

+56
-0
lines changed

2 files changed

+56
-0
lines changed

packages/xgplayer/src/plugins/mobile/index.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ class MobilePlugin extends Plugin {
103103
* @private
104104
*/
105105
this.timer = null
106+
/**
107+
* 标记touch事件状态,供PC插件使用
108+
* @private
109+
*/
110+
this._isTouchActive = false
106111
}
107112

108113
get duration () {
@@ -377,6 +382,14 @@ class MobilePlugin extends Plugin {
377382
onTouchStart = (e) => {
378383
const { player, config, pos, playerConfig } = this
379384
const touche = this.getTouche(e)
385+
386+
// 标记touch事件状态
387+
this._isTouchActive = true
388+
if (this.touchActiveTimer) {
389+
clearTimeout(this.touchActiveTimer)
390+
this.touchActiveTimer = null
391+
}
392+
380393
if (touche && !config.disableGesture && this.duration > 0 && !player.ended) {
381394
pos.isStart = true
382395
this.timer && clearTimeout(this.timer)
@@ -449,6 +462,15 @@ class MobilePlugin extends Plugin {
449462

450463
onTouchEnd = (e) => {
451464
const { player, pos, playerConfig } = this
465+
466+
// 延迟重置touch事件状态,确保后续的mouse事件被忽略
467+
if (this.touchActiveTimer) {
468+
clearTimeout(this.touchActiveTimer)
469+
}
470+
this.touchActiveTimer = setTimeout(() => {
471+
this._isTouchActive = false
472+
}, 10)
473+
452474
setTimeout(() => {
453475
player.getPlugin('progress') && player.getPlugin('progress').resetSeekState()
454476
}, 10)

packages/xgplayer/src/plugins/pc/index.js

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,16 @@ export default class PCPlugin extends BasePlugin {
4545
!enableContextmenu && media && media.addEventListener('contextmenu', this.onContextmenu, false)
4646
}
4747

48+
/**
49+
* 检查Mobile插件是否正在处理touch事件,如果其正在处理touch事件,则不响应mouse事件
50+
* @returns {boolean}
51+
* @private
52+
*/
53+
_isMobileTouchActive() {
54+
const mobilePlugin = this.player?.plugins?.mobile
55+
return !!mobilePlugin && mobilePlugin._isTouchActive === true
56+
}
57+
4858
switchPlayPause (e) {
4959
const { player } = this
5060
this.emitUserAction(e, 'switch_play_pause', { props: 'paused', from: player.paused, to: !player.paused })
@@ -55,15 +65,31 @@ export default class PCPlugin extends BasePlugin {
5565
}
5666
}
5767

68+
/**
69+
* @param {MouseEvent} e
70+
*/
5871
onMouseMove = (e) => {
72+
if (this._isMobileTouchActive()) {
73+
e?.stopImmediatePropagation()
74+
return
75+
}
76+
5977
const { player, playerConfig } = this
6078
if (!player.isActive) {
6179
player.focus({ autoHide: !playerConfig.closeDelayBlur })
6280
!playerConfig.closeFocusVideoFocus && player.media.focus()
6381
}
6482
}
6583

84+
/**
85+
* @param {MouseEvent} e
86+
*/
6687
onMouseEnter = (e) => {
88+
if (this._isMobileTouchActive()) {
89+
e?.stopImmediatePropagation()
90+
return
91+
}
92+
6793
const { playerConfig, player } = this
6894
!playerConfig.closeFocusVideoFocus && player.media.focus()
6995
if (playerConfig.closeDelayBlur) {
@@ -74,7 +100,15 @@ export default class PCPlugin extends BasePlugin {
74100
this.emit(ENTER_PLAYER)
75101
}
76102

103+
/**
104+
* @param {MouseEvent} e
105+
*/
77106
onMouseLeave = (e) => {
107+
if (this._isMobileTouchActive()) {
108+
e?.stopImmediatePropagation()
109+
return
110+
}
111+
78112
const { closePlayerBlur, leavePlayerTime, closeDelayBlur } = this.playerConfig
79113
if (!closePlayerBlur && !closeDelayBlur) {
80114
if (leavePlayerTime) {

0 commit comments

Comments
 (0)