一分钟搭建影视站,支持Docker等部署方式
fix(player): 移动端控制栏常驻,修复 iOS 上按钮来不及点(#937)
bestZwei7cbf4d29433b
diff --git a/src/components/live-player.tsx b/src/components/live-player.tsxindex 93b1a80..9cd6a0e 100644--- a/src/components/live-player.tsx+++ b/src/components/live-player.tsx@@ -4,6 +4,8 @@ import { useEffect, useRef, useState } from 'react';import Artplayer from 'artplayer';import Hls, { type HlsConfig } from 'hls.js';import { Spinner } from './states';+import { prefersTouchPointer } from '@/lib/touch-pointer';+import { ART_DEFAULT_CONTROL_HIDE_MS, TOUCH_CONTROL_HIDE_MS } from '@/lib/control-hide';/*** 直播播放器:与点播 player-shell 完全独立。@@ -290,6 +292,11 @@ export function LivePlayer({ url, title, onPrevChannel, onNextChannel }: LivePlaclick: () => onNextChannel(),});}+ // 触屏没有 hover,原生 3 秒自动隐藏会让直播控制栏(上一台/下一台)点不到+ // (issue #937):触屏常驻、由点按视频 toggle 收起;桌面行为不变+ const touchPointer = prefersTouchPointer();+ if (touchPointer) Artplayer.CONTROL_HIDE_TIME = TOUCH_CONTROL_HIDE_MS;+const art = new Artplayer({container: containerRef.current,url,@@ -366,6 +373,8 @@ export function LivePlayer({ url, title, onPrevChannel, onNextChannel }: LivePlacleanupEngines();art.destroy();artRef.current = null;+ // CONTROL_HIDE_TIME 是全局 static:恢复库默认值,避免影响后续挂载的其它播放器+ if (touchPointer) Artplayer.CONTROL_HIDE_TIME = ART_DEFAULT_CONTROL_HIDE_MS;destroyed = true;};// eslint-disable-next-line react-hooks/exhaustive-depsdiff --git a/src/components/player-shell.tsx b/src/components/player-shell.tsxindex 3598a21..81dd80d 100644--- a/src/components/player-shell.tsx+++ b/src/components/player-shell.tsx@@ -12,6 +12,12 @@ import {} from '@/lib/video-prefetcher';import { loadCacheSettings } from '@/lib/video-cache';import { formatTime } from '@/lib/utils';+import { prefersTouchPointer } from '@/lib/touch-pointer';+import {+ ART_DEFAULT_CONTROL_HIDE_MS,+ DESKTOP_CONTROL_HIDE_MS,+ TOUCH_CONTROL_HIDE_MS,+} from '@/lib/control-hide';/*** 播放器外壳:ArtPlayer + hls.js(旧版 player.js 的 React 化)。@@ -307,9 +313,11 @@ export function PlayerShell({useEffect(() => {if (!containerRef.current || !propsRef.current.url) return;const initialUrl = propsRef.current.url;- // ArtPlayer 默认 3 秒才隐藏控制栏;播放器失去鼠标悬停后缩短为 0.8 秒。- const previousControlHideTime = Artplayer.CONTROL_HIDE_TIME;- Artplayer.CONTROL_HIDE_TIME = 800;+ // ArtPlayer 默认 3 秒才隐藏控制栏;桌面失去鼠标悬停后缩短为 0.8 秒。+ // 触屏没有 hover,自动隐藏会让按钮来不及点(issue #937):改为常驻,+ // 由「点按视频 toggle」收起。CONTROL_HIDE_TIME 是 static,创建后修改立即生效。+ const touchPointer = prefersTouchPointer();+ Artplayer.CONTROL_HIDE_TIME = touchPointer ? TOUCH_CONTROL_HIDE_MS : DESKTOP_CONTROL_HIDE_MS;// 初始化每集状态mediaHealthyRef.current = true;@@ -485,9 +493,14 @@ export function PlayerShell({controlHideTimer = null;art.controls.show = false;};- el?.addEventListener('mousemove', showControlsOnPointerMove);- el?.addEventListener('mouseleave', hideControlsOnMouseLeave);- showControlsOnPointerMove();+ // 触屏不监听 mousemove/mouseleave,也跳过初始显示:iOS 点按视频的合成事件+ // 序列是 mousemove → click,若 mousemove 先把 show 置 true,紧跟的 ArtPlayer+ // click toggle 会把它翻回 false——控制栏「闪一下即隐」,正是 issue #937 的病灶+ if (!touchPointer) {+ el?.addEventListener('mousemove', showControlsOnPointerMove);+ el?.addEventListener('mouseleave', hideControlsOnMouseLeave);+ showControlsOnPointerMove();+ }const onTouchStart = (e: TouchEvent) => {if (art.video?.paused) return;@@ -554,7 +567,7 @@ export function PlayerShell({if (controlHideTimer) clearTimeout(controlHideTimer);restoreCleanupRef.current?.();restoreCleanupRef.current = null;- Artplayer.CONTROL_HIDE_TIME = previousControlHideTime;+ Artplayer.CONTROL_HIDE_TIME = ART_DEFAULT_CONTROL_HIDE_MS;hlsRef.current?.destroy();hlsRef.current = null;recoveryRef.current?.dispose();diff --git a/src/lib/control-hide.test.ts b/src/lib/control-hide.test.tsnew file mode 100644index 0000000..6c45b61--- /dev/null+++ b/src/lib/control-hide.test.ts@@ -0,0 +1,26 @@+import { describe, expect, it } from 'vitest';+import Artplayer from 'artplayer';+import {+ ART_DEFAULT_CONTROL_HIDE_MS,+ DESKTOP_CONTROL_HIDE_MS,+ TOUCH_CONTROL_HIDE_MS,+} from './control-hide';++describe('控制栏隐藏策略', () => {+ it('恢复基准与 ArtPlayer 库默认一致(升级依赖后若默认值变化会在此暴露)', () => {+ expect(ART_DEFAULT_CONTROL_HIDE_MS).toBe(Artplayer.CONTROL_HIDE_TIME);+ });++ it('桌面收紧到 800ms,快于库默认', () => {+ expect(DESKTOP_CONTROL_HIDE_MS).toBe(800);+ expect(DESKTOP_CONTROL_HIDE_MS).toBeLessThan(ART_DEFAULT_CONTROL_HIDE_MS);+ });++ it('触屏常驻:复刻 ArtPlayer 的 timeupdate 判定,永不满足隐藏条件', () => {+ // ArtPlayer: Date.now() - timer >= CONTROL_HIDE_TIME 时 hide+ const timer = Date.now();+ expect(Date.now() - timer >= TOUCH_CONTROL_HIDE_MS).toBe(false);+ // 即便计时器被异常修改到很久以前,也不会溢出成真+ expect(Date.now() - 0 >= TOUCH_CONTROL_HIDE_MS).toBe(false);+ });+});diff --git a/src/lib/control-hide.ts b/src/lib/control-hide.tsnew file mode 100644index 0000000..c84a8b6--- /dev/null+++ b/src/lib/control-hide.ts@@ -0,0 +1,17 @@+/**+ * 控制栏自动隐藏策略(ArtPlayer.CONTROL_HIDE_TIME,全局 static)。+ *+ * 该值是共享状态:恢复时一律回到**库默认值**而不是「进入时的快照」——+ * 点播 / 直播两个播放器先后挂载时,快照可能已经是被上一个组件改过的值+ * (如触屏设为 MAX_SAFE_INTEGER),互相污染会把「触屏常驻」泄漏到桌面。+ * 本应用是唯一写入方,库默认值即安全基准。+ */+export const ART_DEFAULT_CONTROL_HIDE_MS = 3000;+/** 桌面:鼠标停止移动 / 离开播放器后快速收起 */+export const DESKTOP_CONTROL_HIDE_MS = 800;+/**+ * 触屏:永不自动隐藏(issue LibreSpark/LibreTV#937)。+ * ArtPlayer 的判定是 `Date.now() - timer >= CONTROL_HIDE_TIME`,该差值是毫秒级,+ * 恒小于 MAX_SAFE_INTEGER,因此自动隐藏永不触发——由「点按视频 toggle」收起。+ */+export const TOUCH_CONTROL_HIDE_MS = Number.MAX_SAFE_INTEGER;diff --git a/src/lib/touch-pointer.test.ts b/src/lib/touch-pointer.test.tsnew file mode 100644index 0000000..64a49a7--- /dev/null+++ b/src/lib/touch-pointer.test.ts@@ -0,0 +1,50 @@+import { afterEach, describe, expect, it, vi } from 'vitest';+import { prefersTouchPointer } from './touch-pointer';++afterEach(() => {+ vi.unstubAllGlobals();+});++const mq = (matches: boolean) => () => ({ matches });++describe('prefersTouchPointer', () => {+ it('hover:none + pointer:coarse(手机/平板手指)→ 触屏', () => {+ expect(prefersTouchPointer(mq(true))).toBe(true);+ });++ it('hover 可用(桌面鼠标/触控板)→ 非触屏', () => {+ expect(prefersTouchPointer(mq(false))).toBe(false);+ });++ it('查询串固定为 (hover: none) and (pointer: coarse)', () => {+ const queries: string[] = [];+ prefersTouchPointer((q) => {+ queries.push(q);+ return { matches: true };+ });+ expect(queries).toEqual(['(hover: none) and (pointer: coarse)']);+ });++ it('SSR / 无 window → 按桌面处理(node 环境天然覆盖)', () => {+ expect(prefersTouchPointer()).toBe(false);+ });++ it('window 有 matchMedia 时走真实媒体查询结果', () => {+ vi.stubGlobal('window', { matchMedia: () => ({ matches: true }) });+ expect(prefersTouchPointer()).toBe(true);+ });++ it('window 存在但无 matchMedia(老浏览器)→ 桌面处理', () => {+ vi.stubGlobal('window', {});+ expect(prefersTouchPointer()).toBe(false);+ });++ it('matchMedia 抛错(隐私模式等异常环境)→ 桌面处理,不炸调用方', () => {+ vi.stubGlobal('window', {+ matchMedia: () => {+ throw new Error('boom');+ },+ });+ expect(prefersTouchPointer()).toBe(false);+ });+});diff --git a/src/lib/touch-pointer.ts b/src/lib/touch-pointer.tsnew file mode 100644index 0000000..6f38e45--- /dev/null+++ b/src/lib/touch-pointer.ts@@ -0,0 +1,23 @@+/**+ * 触屏指针判定。+ *+ * 用途:播放器控制栏交互分流(issue LibreSpark/LibreTV#937)——+ * 触屏设备没有 hover,控制栏只能靠「点按视频 toggle」显示/收起,+ * 任何自动隐藏(ArtPlayer 按 CONTROL_HIDE_TIME 计时收起)都会让按钮点不到;+ * 桌面鼠标维持「mousemove 显示 + 失去悬停收起」。+ *+ * 用媒体查询而非 UA 嗅探:`(hover: none) and (pointer: coarse)` 对+ * iPad 鼠标模式 / 触屏笔记本等混合形态的语义比解析 UA 可靠。+ */+export function prefersTouchPointer(+ match?: (query: string) => { matches: boolean }+): boolean {+ try {+ if (match) return match('(hover: none) and (pointer: coarse)').matches;+ // SSR / 老浏览器没有 matchMedia:按桌面(非触屏)处理,行为与现状一致+ if (typeof window === 'undefined' || typeof window.matchMedia !== 'function') return false;+ return window.matchMedia('(hover: none) and (pointer: coarse)').matches;+ } catch {+ return false;+ }+}