Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cb30888899 |
+5
-2
@@ -9,9 +9,12 @@ import type {
|
||||
export const APP_VERSION = __APP_VERSION__;
|
||||
|
||||
const ua = navigator.userAgent;
|
||||
export const isAndroid = /Android/i.test(ua);
|
||||
/** iPhone 的 UA 也带「like Mac OS X」,必须先于 macOS 判断;iPadOS 默认伪装成 Mac,用触点数区分。 */
|
||||
export const isIos = /iPhone|iPad|iPod/i.test(ua) || (/Macintosh/i.test(ua) && navigator.maxTouchPoints > 1);
|
||||
export const isIos =
|
||||
/iPhone|iPad|iPod/i.test(ua) ||
|
||||
(/Macintosh/i.test(ua) && navigator.maxTouchPoints > 1) ||
|
||||
(typeof navigator.platform === "string" && navigator.platform === "MacIntel" && navigator.maxTouchPoints > 1);
|
||||
export const isAndroid = /Android/i.test(ua);
|
||||
export const isMacos = !isIos && /Macintosh|Mac OS X/i.test(ua);
|
||||
export const platform: "android" | "ios" | "windows" | "macos" =
|
||||
isAndroid ? "android" : isIos ? "ios" : isMacos ? "macos" : "windows";
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
import React, { useEffect, useRef, useState } from "react";
|
||||
import Hls from "hls.js";
|
||||
import { fmtTime, playlist } from "../api";
|
||||
import { fmtTime, platform, playlist } from "../api";
|
||||
import { mediaResource, observeHls, usePlayerDiagnostics } from "../playbackDiagnostics";
|
||||
import { usePlaybackActivity } from "../usePlaybackActivity";
|
||||
import { useSeekInput } from "../useSeekInput";
|
||||
@@ -30,6 +30,7 @@ export default function Player({
|
||||
const videoRef = useRef<HTMLVideoElement>(null);
|
||||
const wrapRef = useRef<HTMLDivElement>(null);
|
||||
const hlsRef = useRef<Hls | null>(null);
|
||||
const recoverCountRef = useRef(0);
|
||||
const { trace, requestSeek } = usePlayerDiagnostics(videoRef, hlsRef, { view: "post_detail", ...diagnosticContext, active, resource: mediaResource(src) });
|
||||
const activity = usePlaybackActivity(videoRef, hlsRef, { active, autoPlay: !!autoPlay, src, trace });
|
||||
const [playing, setPlaying] = useState(false);
|
||||
@@ -46,6 +47,10 @@ export default function Player({
|
||||
const video = videoRef.current;
|
||||
if (!video || !activity.isActive()) return;
|
||||
requestSeek(target, `legacy_${input}`);
|
||||
setWaiting(true);
|
||||
if (hlsRef.current) {
|
||||
try { hlsRef.current.startLoad(target); } catch { /* ignore */ }
|
||||
}
|
||||
video.currentTime = target;
|
||||
trace("seek.assigned", { target });
|
||||
setTime(video.currentTime);
|
||||
@@ -56,6 +61,8 @@ export default function Player({
|
||||
const video = videoRef.current;
|
||||
if (!video || !src) return;
|
||||
const url = playlist(src);
|
||||
recoverCountRef.current = 0;
|
||||
setErrMsg("");
|
||||
trace("source.load", { engine: Hls.isSupported() ? "hls.js" : "native", auto_play: autoPlay });
|
||||
|
||||
if (hlsRef.current) { hlsRef.current.destroy(); hlsRef.current = null; }
|
||||
@@ -66,16 +73,22 @@ export default function Player({
|
||||
interstitialsController: undefined,
|
||||
contentSteeringController: undefined,
|
||||
enableInterstitialPlayback: false,
|
||||
enableWorker: true,
|
||||
enableWorker: platform !== "ios",
|
||||
// Keep the same macOS seek workaround as the shared player.
|
||||
preferManagedMediaSource: false,
|
||||
autoStartLoad: false,
|
||||
lowLatencyMode: false,
|
||||
maxBufferLength: 24,
|
||||
maxBufferLength: 30,
|
||||
maxMaxBufferLength: 60,
|
||||
backBufferLength: 30,
|
||||
maxBufferHole: 0.5,
|
||||
nudgeOffset: 0.25,
|
||||
nudgeMaxRetry: 6,
|
||||
manifestLoadingTimeOut: 20000,
|
||||
manifestLoadingMaxRetry: 3,
|
||||
fragLoadingMaxRetry: 4,
|
||||
manifestLoadingMaxRetry: 4,
|
||||
fragLoadingTimeOut: 25000,
|
||||
fragLoadingMaxRetry: 6,
|
||||
fragLoadingRetryDelay: 400,
|
||||
});
|
||||
hlsRef.current = hls;
|
||||
observeHls(hls, trace);
|
||||
@@ -85,12 +98,34 @@ export default function Player({
|
||||
if (activity.isActive()) hls.startLoad();
|
||||
if (autoPlay) activity.play("manifest_autoplay", true);
|
||||
});
|
||||
hls.on(Hls.Events.FRAG_BUFFERED, () => {
|
||||
recoverCountRef.current = 0;
|
||||
setErrMsg("");
|
||||
});
|
||||
hls.on(Hls.Events.ERROR, (_e, data) => {
|
||||
if (!data.fatal) return;
|
||||
if (!data.fatal) {
|
||||
if (
|
||||
data.details === Hls.ErrorDetails.BUFFER_STALLED_ERROR ||
|
||||
data.details === Hls.ErrorDetails.FRAG_LOAD_ERROR
|
||||
) {
|
||||
try { hls.startLoad(video.currentTime || -1); } catch { /* ignore */ }
|
||||
}
|
||||
return;
|
||||
}
|
||||
if (!activity.isActive()) return;
|
||||
if (data.type === Hls.ErrorTypes.NETWORK_ERROR && recoverCountRef.current < 4) {
|
||||
recoverCountRef.current += 1;
|
||||
try { hls.startLoad(video.currentTime || -1); } catch { /* ignore */ }
|
||||
return;
|
||||
}
|
||||
if (data.type === Hls.ErrorTypes.MEDIA_ERROR && recoverCountRef.current < 4) {
|
||||
recoverCountRef.current += 1;
|
||||
try { hls.recoverMediaError(); video.play().catch(() => {}); } catch { /* ignore */ }
|
||||
return;
|
||||
}
|
||||
const detail = `${data.type}/${data.details}/${data.response?.code ?? ""}/${String(data.reason ?? "").slice(0, 80)}`;
|
||||
(window as unknown as { __hlsErr?: string }).__hlsErr = detail;
|
||||
setErrMsg(detail);
|
||||
if (!activity.isActive()) return;
|
||||
if (data.type === Hls.ErrorTypes.NETWORK_ERROR) hls.startLoad();
|
||||
else if (data.type === Hls.ErrorTypes.MEDIA_ERROR) hls.recoverMediaError();
|
||||
else { hls.destroy(); hlsRef.current = null; }
|
||||
@@ -151,6 +186,8 @@ export default function Player({
|
||||
ref={videoRef}
|
||||
poster={poster}
|
||||
playsInline
|
||||
webkit-playsinline="true"
|
||||
preload="auto"
|
||||
muted={muted}
|
||||
loop={loop}
|
||||
className="w-full h-full object-contain bg-black"
|
||||
@@ -158,7 +195,8 @@ export default function Player({
|
||||
onPlay={() => { if (activity.isActive()) { setPlaying(true); onTogglePlay?.(true); } }}
|
||||
onPause={() => { setPlaying(false); onTogglePlay?.(false); }}
|
||||
onWaiting={() => setWaiting(true)}
|
||||
onPlaying={() => { if (activity.isActive()) { setWaiting(false); onPlaying?.(); } }}
|
||||
onPlaying={() => { if (activity.isActive()) { setWaiting(false); setErrMsg(""); onPlaying?.(); } }}
|
||||
onSeeked={() => setWaiting(false)}
|
||||
onTimeUpdate={(e) => setTime((e.target as HTMLVideoElement).currentTime)}
|
||||
onLoadedMetadata={(e) => setDuration((e.target as HTMLVideoElement).duration || 0)}
|
||||
onEnded={() => { if (activity.isActive()) onEnded?.(); }}
|
||||
|
||||
+269
-44
@@ -1,6 +1,6 @@
|
||||
import { useEffect, useImperativeHandle, useRef, forwardRef, useState } from "react";
|
||||
import React, { useEffect, useImperativeHandle, useRef, forwardRef, useState } from "react";
|
||||
import Hls from "hls.js";
|
||||
import { isLocalMedia, playlist } from "../api";
|
||||
import { isLocalMedia, platform, playlist } from "../api";
|
||||
import { mediaResource, observeHls, usePlayerDiagnostics } from "../playbackDiagnostics";
|
||||
import { usePlaybackActivity } from "../usePlaybackActivity";
|
||||
|
||||
@@ -9,11 +9,12 @@ export type VideoHandle = {
|
||||
play: () => void;
|
||||
pause: () => void;
|
||||
toggle: () => void;
|
||||
seek: (t: number, source?: string) => void;
|
||||
seek: (t: number, sourceOrAutoResume?: string | boolean) => void;
|
||||
trace: (event: string, data?: Record<string, unknown>) => void;
|
||||
rate: (r: number) => void;
|
||||
mute: (m: boolean) => void;
|
||||
volume: (v: number) => void;
|
||||
reload: () => void;
|
||||
};
|
||||
|
||||
type Props = {
|
||||
@@ -25,6 +26,11 @@ type Props = {
|
||||
muted?: boolean;
|
||||
loop?: boolean;
|
||||
fit?: "contain" | "cover";
|
||||
/** 缩放倍率与平移偏移(用于移动端竖屏下双指缩放横屏视频) */
|
||||
scale?: number;
|
||||
panX?: number;
|
||||
panY?: number;
|
||||
isGesturing?: boolean;
|
||||
/** 换源后重新套用,避免切集时音量/倍速被浏览器重置 */
|
||||
volume?: number;
|
||||
rate?: number;
|
||||
@@ -42,16 +48,75 @@ type Props = {
|
||||
|
||||
/** 只负责「播」:hls.js + video 元素,控制条由外层渲染。 */
|
||||
const VideoSurface = forwardRef<VideoHandle, Props>(function VideoSurface(
|
||||
{ src, poster, autoPlay = true, active = true, diagnosticContext = {}, muted = false, loop = false, fit = "contain", volume = 1, rate = 1,
|
||||
{ src, poster, autoPlay = true, active = true, diagnosticContext = {}, muted = false, loop = false, fit = "contain",
|
||||
scale = 1, panX = 0, panY = 0, isGesturing = false,
|
||||
volume = 1, rate = 1,
|
||||
onTime, onDuration, onSize, onPlaying, onPause, onWaiting, onEnded, onError, onClick, className = "" },
|
||||
ref,
|
||||
) {
|
||||
const videoRef = useRef<HTMLVideoElement>(null);
|
||||
const hlsRef = useRef<Hls | null>(null);
|
||||
const recoverCountRef = useRef(0);
|
||||
const recoverTimerRef = useRef<number | null>(null);
|
||||
const lastSizeRef = useRef({ w: 0, h: 0 });
|
||||
const [ready, setReady] = useState(false);
|
||||
const { trace, requestSeek } = usePlayerDiagnostics(videoRef, hlsRef, { ...diagnosticContext, active, resource: mediaResource(src) });
|
||||
const activity = usePlaybackActivity(videoRef, hlsRef, { active, autoPlay, src, trace });
|
||||
|
||||
const reportSizeAndDuration = (v: HTMLVideoElement) => {
|
||||
if (v.duration && Number.isFinite(v.duration) && v.duration > 0) {
|
||||
onDuration?.(v.duration);
|
||||
}
|
||||
const w = v.videoWidth || 0;
|
||||
const h = v.videoHeight || 0;
|
||||
if (w > 0 && h > 0 && (w !== lastSizeRef.current.w || h !== lastSizeRef.current.h)) {
|
||||
lastSizeRef.current = { w, h };
|
||||
onSize?.(w, h);
|
||||
}
|
||||
};
|
||||
|
||||
const safeSeek = (t: number, sourceOrAutoResume: string | boolean = "handle") => {
|
||||
const v = videoRef.current;
|
||||
if (!v || !Number.isFinite(t)) return;
|
||||
const dur = v.duration && Number.isFinite(v.duration) ? v.duration : 0;
|
||||
const target = dur > 0 ? Math.max(0, Math.min(dur - 0.15, t)) : Math.max(0, t);
|
||||
const source = typeof sourceOrAutoResume === "string" ? sourceOrAutoResume : "handle";
|
||||
const autoResume = typeof sourceOrAutoResume === "boolean" ? sourceOrAutoResume : !v.paused;
|
||||
|
||||
requestSeek(target, source);
|
||||
onWaiting?.();
|
||||
if (hlsRef.current) {
|
||||
try {
|
||||
hlsRef.current.startLoad(target);
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
|
||||
try {
|
||||
v.currentTime = target;
|
||||
trace("seek.assigned", { target, source });
|
||||
} catch (e) {
|
||||
trace("seek.rejected", { error_name: (e as Error).name });
|
||||
}
|
||||
|
||||
if (autoResume && activity.isActive()) {
|
||||
const resumeOnce = () => {
|
||||
v.removeEventListener("seeked", resumeOnce);
|
||||
v.removeEventListener("canplay", resumeOnce);
|
||||
if (activity.isActive()) v.play().catch(() => {});
|
||||
};
|
||||
v.addEventListener("seeked", resumeOnce, { once: true });
|
||||
v.addEventListener("canplay", resumeOnce, { once: true });
|
||||
// 兜底:若 350ms 内已经就绪则直接播
|
||||
window.setTimeout(() => {
|
||||
if (v.readyState >= 2 && v.paused && activity.isActive()) {
|
||||
v.removeEventListener("seeked", resumeOnce);
|
||||
v.removeEventListener("canplay", resumeOnce);
|
||||
v.play().catch(() => {});
|
||||
}
|
||||
}, 350);
|
||||
}
|
||||
};
|
||||
|
||||
useImperativeHandle(ref, () => ({
|
||||
el: () => videoRef.current,
|
||||
play: () => activity.play("handle"),
|
||||
@@ -62,29 +127,47 @@ const VideoSurface = forwardRef<VideoHandle, Props>(function VideoSurface(
|
||||
trace("toggle.request");
|
||||
if (v.paused) activity.play("toggle"); else activity.pause("toggle");
|
||||
},
|
||||
seek: (t: number, source = "handle") => {
|
||||
const v = videoRef.current;
|
||||
if (v && Number.isFinite(t)) {
|
||||
requestSeek(t, source);
|
||||
try { v.currentTime = t; trace("seek.assigned", { target: t, source }); }
|
||||
catch (e) { trace("seek.rejected", { error_name: (e as Error).name }); throw e; }
|
||||
}
|
||||
},
|
||||
seek: (t: number, sourceOrAutoResume: string | boolean = "handle") => safeSeek(t, sourceOrAutoResume),
|
||||
trace,
|
||||
rate: (r: number) => { const v = videoRef.current; if (v) v.playbackRate = r; },
|
||||
mute: (m: boolean) => { const v = videoRef.current; if (v) v.muted = m; },
|
||||
volume: (vol: number) => { const v = videoRef.current; if (v) v.volume = vol; },
|
||||
}), []);
|
||||
reload: () => {
|
||||
const v = videoRef.current;
|
||||
if (!v) return;
|
||||
recoverCountRef.current = 0;
|
||||
const cur = v.currentTime || 0;
|
||||
if (hlsRef.current) {
|
||||
try {
|
||||
hlsRef.current.startLoad(cur);
|
||||
activity.play("reload");
|
||||
return;
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
v.load();
|
||||
if (cur > 0) {
|
||||
try { v.currentTime = cur; } catch { /* ignore */ }
|
||||
}
|
||||
activity.play("reload");
|
||||
},
|
||||
}));
|
||||
|
||||
useEffect(() => {
|
||||
const video = videoRef.current;
|
||||
if (!video || !src) return;
|
||||
setReady(false);
|
||||
recoverCountRef.current = 0;
|
||||
lastSizeRef.current = { w: 0, h: 0 };
|
||||
if (recoverTimerRef.current) {
|
||||
window.clearTimeout(recoverTimerRef.current);
|
||||
recoverTimerRef.current = null;
|
||||
}
|
||||
if (hlsRef.current) { hlsRef.current.destroy(); hlsRef.current = null; }
|
||||
|
||||
const proxied = isLocalMedia(src);
|
||||
const url = proxied ? src : playlist(src);
|
||||
const useHls = !url.includes("/seg?u=");
|
||||
const isIos = platform === "ios";
|
||||
trace("source.load", { engine: useHls && Hls.isSupported() ? "hls.js" : "native", auto_play: autoPlay, resource: mediaResource(src) });
|
||||
|
||||
/* 换源时先把音量/倍速套回去:hls 重新 attach 后浏览器会把它们重置成默认值 */
|
||||
@@ -103,15 +186,27 @@ const VideoSurface = forwardRef<VideoHandle, Props>(function VideoSurface(
|
||||
interstitialsController: undefined,
|
||||
contentSteeringController: undefined,
|
||||
enableInterstitialPlayback: false,
|
||||
enableWorker: true,
|
||||
// iOS WKWebView 下 WebWorker 跨自定义协议 hg:// 高频中断易死锁,关闭 Worker 走主线程加载更稳
|
||||
enableWorker: !isIos,
|
||||
// Prefer standard MSE on macOS; hls.js handles MMS-only fallback and seek recovery.
|
||||
preferManagedMediaSource: false,
|
||||
autoStartLoad: false,
|
||||
lowLatencyMode: false,
|
||||
maxBufferLength: 30,
|
||||
maxMaxBufferLength: 60,
|
||||
backBufferLength: 30,
|
||||
maxBufferHole: 0.5,
|
||||
highBufferWatchdogPeriod: 2,
|
||||
nudgeOffset: 0.25,
|
||||
nudgeMaxRetry: 6,
|
||||
manifestLoadingTimeOut: 20000,
|
||||
manifestLoadingMaxRetry: 3,
|
||||
fragLoadingMaxRetry: 4,
|
||||
manifestLoadingMaxRetry: 4,
|
||||
levelLoadingTimeOut: 20000,
|
||||
levelLoadingMaxRetry: 4,
|
||||
fragLoadingTimeOut: 25000,
|
||||
fragLoadingMaxRetry: 6,
|
||||
fragLoadingRetryDelay: 400,
|
||||
startFragPrefetch: true,
|
||||
});
|
||||
hlsRef.current = hls;
|
||||
observeHls(hls, trace);
|
||||
@@ -121,13 +216,63 @@ const VideoSurface = forwardRef<VideoHandle, Props>(function VideoSurface(
|
||||
if (activity.isActive()) hls.startLoad();
|
||||
if (autoPlay) activity.play("manifest_autoplay", true);
|
||||
});
|
||||
hls.on(Hls.Events.FRAG_BUFFERED, () => {
|
||||
recoverCountRef.current = 0;
|
||||
setReady(true);
|
||||
});
|
||||
hls.on(Hls.Events.ERROR, (_e, data) => {
|
||||
if (!data.fatal) return;
|
||||
onError?.(`${data.type}/${data.details}`);
|
||||
// 非致命卡顿错误(常见于拖动进度条后缓冲空洞):主动触发加载与微调跳帧
|
||||
if (!data.fatal) {
|
||||
if (
|
||||
activity.isActive() && (
|
||||
data.details === Hls.ErrorDetails.BUFFER_STALLED_ERROR ||
|
||||
data.details === Hls.ErrorDetails.FRAG_LOAD_ERROR ||
|
||||
data.details === Hls.ErrorDetails.FRAG_LOAD_TIMEOUT
|
||||
)
|
||||
) {
|
||||
try {
|
||||
hls.startLoad(video.currentTime || -1);
|
||||
} catch { /* ignore */ }
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (!activity.isActive()) return;
|
||||
if (data.type === Hls.ErrorTypes.NETWORK_ERROR) hls.startLoad();
|
||||
else if (data.type === Hls.ErrorTypes.MEDIA_ERROR) hls.recoverMediaError();
|
||||
else { hls.destroy(); hlsRef.current = null; }
|
||||
|
||||
// 致命错误:优先自动重连/恢复解码器,最多静默恢复 4 次,避免拖动进度条直接弹报错层
|
||||
if (data.type === Hls.ErrorTypes.NETWORK_ERROR) {
|
||||
if (recoverCountRef.current < 4) {
|
||||
recoverCountRef.current += 1;
|
||||
const delay = 250 * recoverCountRef.current;
|
||||
if (recoverTimerRef.current) window.clearTimeout(recoverTimerRef.current);
|
||||
recoverTimerRef.current = window.setTimeout(() => {
|
||||
if (!hlsRef.current || !activity.isActive()) return;
|
||||
try {
|
||||
hlsRef.current.startLoad(video.currentTime || -1);
|
||||
if (!video.paused) video.play().catch(() => {});
|
||||
} catch { /* ignore */ }
|
||||
}, delay);
|
||||
return;
|
||||
}
|
||||
onError?.(`${data.type}/${data.details}`);
|
||||
hls.startLoad();
|
||||
} else if (data.type === Hls.ErrorTypes.MEDIA_ERROR) {
|
||||
if (recoverCountRef.current < 4) {
|
||||
recoverCountRef.current += 1;
|
||||
try {
|
||||
if (recoverCountRef.current === 2) hls.swapAudioCodec();
|
||||
hls.recoverMediaError();
|
||||
video.play().catch(() => {});
|
||||
} catch { /* ignore */ }
|
||||
return;
|
||||
}
|
||||
onError?.(`${data.type}/${data.details}`);
|
||||
hls.recoverMediaError();
|
||||
} else {
|
||||
onError?.(`${data.type}/${data.details}`);
|
||||
hls.destroy();
|
||||
hlsRef.current = null;
|
||||
}
|
||||
});
|
||||
} else {
|
||||
video.src = url;
|
||||
@@ -136,6 +281,10 @@ const VideoSurface = forwardRef<VideoHandle, Props>(function VideoSurface(
|
||||
|
||||
return () => {
|
||||
trace("source.cleanup");
|
||||
if (recoverTimerRef.current) {
|
||||
window.clearTimeout(recoverTimerRef.current);
|
||||
recoverTimerRef.current = null;
|
||||
}
|
||||
if (hlsRef.current) { hlsRef.current.destroy(); hlsRef.current = null; }
|
||||
};
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
@@ -151,30 +300,106 @@ const VideoSurface = forwardRef<VideoHandle, Props>(function VideoSurface(
|
||||
v.playbackRate = rate;
|
||||
};
|
||||
|
||||
const nearEndFiredRef = useRef(false);
|
||||
useEffect(() => {
|
||||
nearEndFiredRef.current = false;
|
||||
}, [src]);
|
||||
|
||||
// Native Resize Event listener for HTMLVideoElement (React doesn't always bind onResize on <video>)
|
||||
useEffect(() => {
|
||||
const v = videoRef.current;
|
||||
if (!v) return undefined;
|
||||
const onVideoResize = () => reportSizeAndDuration(v);
|
||||
v.addEventListener("resize", onVideoResize);
|
||||
return () => v.removeEventListener("resize", onVideoResize);
|
||||
// eslint-disable-next-line react-hooks/exhaustive-deps
|
||||
}, [src]);
|
||||
|
||||
const hasTransform = scale !== 1 || panX !== 0 || panY !== 0;
|
||||
|
||||
return (
|
||||
<video
|
||||
ref={videoRef}
|
||||
poster={poster}
|
||||
playsInline
|
||||
muted={muted}
|
||||
loop={loop}
|
||||
onClick={onClick}
|
||||
className={`w-full h-full bg-black ${fit === "cover" ? "object-cover" : "object-contain"} ${className}`}
|
||||
style={{ opacity: ready ? 1 : 0, transition: "opacity .35s" }}
|
||||
onLoadedData={(e) => { setReady(true); reapply(e.target as HTMLVideoElement); }}
|
||||
onTimeUpdate={(e) => onTime?.((e.target as HTMLVideoElement).currentTime)}
|
||||
onLoadedMetadata={(e) => {
|
||||
const v = e.target as HTMLVideoElement;
|
||||
reapply(v);
|
||||
onDuration?.(v.duration || 0);
|
||||
onSize?.(v.videoWidth || 0, v.videoHeight || 0);
|
||||
}}
|
||||
onPlay={() => { if (activity.isActive()) onPlaying?.(); }}
|
||||
onPause={() => onPause?.()}
|
||||
onWaiting={() => onWaiting?.()}
|
||||
onPlaying={() => { if (activity.isActive()) onPlaying?.(); }}
|
||||
onEnded={() => { if (activity.isActive()) onEnded?.(); }}
|
||||
/>
|
||||
<div className={`relative w-full h-full bg-black overflow-hidden ${className}`}>
|
||||
{!ready && poster && (
|
||||
<img
|
||||
src={poster}
|
||||
alt=""
|
||||
className={`absolute inset-0 w-full h-full pointer-events-none ${fit === "cover" ? "object-cover" : "object-contain"}`}
|
||||
/>
|
||||
)}
|
||||
<video
|
||||
ref={videoRef}
|
||||
playsInline
|
||||
webkit-playsinline="true"
|
||||
preload="auto"
|
||||
muted={muted}
|
||||
loop={loop}
|
||||
onClick={onClick}
|
||||
className={`w-full h-full bg-black ${fit === "cover" ? "object-cover" : "object-contain"}`}
|
||||
style={{
|
||||
opacity: ready ? 1 : 0,
|
||||
transform: hasTransform ? `translate3d(${panX}px, ${panY}px, 0) scale(${scale})` : "translateZ(0)",
|
||||
transformOrigin: "center center",
|
||||
transition: isGesturing ? "opacity .25s" : "opacity .25s, transform .2s cubic-bezier(.2,0,0,1)",
|
||||
}}
|
||||
onLoadedData={(e) => {
|
||||
const v = e.target as HTMLVideoElement;
|
||||
setReady(true);
|
||||
reapply(v);
|
||||
reportSizeAndDuration(v);
|
||||
}}
|
||||
onCanPlay={(e) => {
|
||||
const v = e.target as HTMLVideoElement;
|
||||
setReady(true);
|
||||
reportSizeAndDuration(v);
|
||||
}}
|
||||
onTimeUpdate={(e) => {
|
||||
const v = e.target as HTMLVideoElement;
|
||||
onTime?.(v.currentTime);
|
||||
if (lastSizeRef.current.w === 0 && v.videoWidth > 0) {
|
||||
reportSizeAndDuration(v);
|
||||
}
|
||||
// 借鉴 stclock1988/hghub-ios:部分 HLS 流容器标称时长比最后一个分片略长 0.5~1.2s,
|
||||
// 接近片尾时提前复位准备,若卡在片尾则自动触发连播
|
||||
if (!loop && !nearEndFiredRef.current && activity.isActive() && v.duration > 5 && v.currentTime >= v.duration - 0.35) {
|
||||
nearEndFiredRef.current = true;
|
||||
onEnded?.();
|
||||
}
|
||||
}}
|
||||
onLoadedMetadata={(e) => {
|
||||
const v = e.target as HTMLVideoElement;
|
||||
reapply(v);
|
||||
reportSizeAndDuration(v);
|
||||
}}
|
||||
onPlay={() => { if (activity.isActive()) onPlaying?.(); }}
|
||||
onPause={() => onPause?.()}
|
||||
onWaiting={(e) => {
|
||||
const v = e.target as HTMLVideoElement;
|
||||
if (!loop && !nearEndFiredRef.current && activity.isActive() && v.duration > 5 && v.currentTime >= v.duration - 1.5) {
|
||||
nearEndFiredRef.current = true;
|
||||
onEnded?.();
|
||||
return;
|
||||
}
|
||||
onWaiting?.();
|
||||
}}
|
||||
onPlaying={(e) => {
|
||||
setReady(true);
|
||||
recoverCountRef.current = 0;
|
||||
reportSizeAndDuration(e.target as HTMLVideoElement);
|
||||
if (activity.isActive()) onPlaying?.();
|
||||
}}
|
||||
onSeeking={() => onWaiting?.()}
|
||||
onSeeked={() => {
|
||||
const v = videoRef.current;
|
||||
if (v && !v.paused && activity.isActive()) onPlaying?.();
|
||||
}}
|
||||
onEnded={() => {
|
||||
if (!nearEndFiredRef.current && activity.isActive()) {
|
||||
nearEndFiredRef.current = true;
|
||||
onEnded?.();
|
||||
}
|
||||
}}
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user