@nikelroot/video-player 0.2.6 → 0.2.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +24 -5
- package/dist/index.js +24 -5
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -862,6 +862,7 @@ var VideoPlayerBase = (props, ref) => {
|
|
|
862
862
|
const controlTime = (0, import_react3.useRef)(null);
|
|
863
863
|
const sourceLoadId = (0, import_react3.useRef)(0);
|
|
864
864
|
const sourceLoadMode = (0, import_react3.useRef)("native");
|
|
865
|
+
const pendingPlay = (0, import_react3.useRef)(false);
|
|
865
866
|
const [playingState, setPlayingState] = (0, import_react3.useState)(!!defaultPlaying);
|
|
866
867
|
const [mutedState, setMutedState] = (0, import_react3.useState)(!!defaultMuted);
|
|
867
868
|
const [durationState, setDurationState] = (0, import_react3.useState)(0);
|
|
@@ -907,6 +908,7 @@ var VideoPlayerBase = (props, ref) => {
|
|
|
907
908
|
}, []);
|
|
908
909
|
const resetMediaForSourceChange = (0, import_react3.useCallback)(() => {
|
|
909
910
|
clearTimers();
|
|
911
|
+
pendingPlay.current = false;
|
|
910
912
|
if (hlsRef.current) {
|
|
911
913
|
hlsRef.current.destroy();
|
|
912
914
|
hlsRef.current = null;
|
|
@@ -920,6 +922,7 @@ var VideoPlayerBase = (props, ref) => {
|
|
|
920
922
|
const releaseNativeMedia = (0, import_react3.useCallback)(() => {
|
|
921
923
|
const video = videoRef.current;
|
|
922
924
|
if (!video) return;
|
|
925
|
+
pendingPlay.current = false;
|
|
923
926
|
video.pause();
|
|
924
927
|
if (sourceLoadMode.current !== "native" || !isVideoSourceAttached(video)) return;
|
|
925
928
|
video.removeAttribute("src");
|
|
@@ -927,9 +930,11 @@ var VideoPlayerBase = (props, ref) => {
|
|
|
927
930
|
}, []);
|
|
928
931
|
const ensureNativeMediaAttached = (0, import_react3.useCallback)(() => {
|
|
929
932
|
const video = videoRef.current;
|
|
930
|
-
if (!video || sourceLoadMode.current !== "native" || isVideoSourceAttached(video)) return;
|
|
933
|
+
if (!video || sourceLoadMode.current !== "native" || isVideoSourceAttached(video)) return false;
|
|
931
934
|
video.src = videoSrc;
|
|
932
935
|
if (initialTime > 0) video.currentTime = initialTime;
|
|
936
|
+
video.load();
|
|
937
|
+
return true;
|
|
933
938
|
}, [initialTime, videoSrc]);
|
|
934
939
|
const setNextPlaying = (0, import_react3.useCallback)(
|
|
935
940
|
(value) => {
|
|
@@ -1029,15 +1034,25 @@ var VideoPlayerBase = (props, ref) => {
|
|
|
1029
1034
|
const playAction = (0, import_react3.useCallback)(async () => {
|
|
1030
1035
|
const el = videoRef.current;
|
|
1031
1036
|
if (!el) return;
|
|
1037
|
+
pendingPlay.current = true;
|
|
1032
1038
|
pauseOtherVideos(el);
|
|
1033
|
-
ensureNativeMediaAttached();
|
|
1039
|
+
const attachedNativeMedia = ensureNativeMediaAttached();
|
|
1040
|
+
if (sourceLoadMode.current === "native" && !canAttemptPlayback(el)) {
|
|
1041
|
+
if (attachedNativeMedia) clearErrorState();
|
|
1042
|
+
return;
|
|
1043
|
+
}
|
|
1034
1044
|
try {
|
|
1035
1045
|
await el.play();
|
|
1046
|
+
pendingPlay.current = false;
|
|
1036
1047
|
clearErrorState();
|
|
1037
1048
|
setNextPlaying(true);
|
|
1038
1049
|
notifyActiveChange(el, "play");
|
|
1039
1050
|
} catch (e) {
|
|
1040
|
-
if (isInterruptedPlayRequestError(e))
|
|
1051
|
+
if (isInterruptedPlayRequestError(e)) {
|
|
1052
|
+
pendingPlay.current = true;
|
|
1053
|
+
return;
|
|
1054
|
+
}
|
|
1055
|
+
pendingPlay.current = false;
|
|
1041
1056
|
console.debug(e);
|
|
1042
1057
|
reportError(e, { type: "play" }, messages.playFailed);
|
|
1043
1058
|
}
|
|
@@ -1065,6 +1080,7 @@ var VideoPlayerBase = (props, ref) => {
|
|
|
1065
1080
|
const pauseAction = (0, import_react3.useCallback)(() => {
|
|
1066
1081
|
const el = videoRef.current;
|
|
1067
1082
|
if (!el) return;
|
|
1083
|
+
pendingPlay.current = false;
|
|
1068
1084
|
el.pause();
|
|
1069
1085
|
setNextPlaying(false);
|
|
1070
1086
|
notifyActiveChange(el, "pause");
|
|
@@ -1393,11 +1409,14 @@ var VideoPlayerBase = (props, ref) => {
|
|
|
1393
1409
|
const error = video == null ? void 0 : video.error;
|
|
1394
1410
|
const src = (video == null ? void 0 : video.currentSrc) || videoSrc;
|
|
1395
1411
|
if (hlsRef.current && src.startsWith("blob:")) return;
|
|
1412
|
+
pendingPlay.current = false;
|
|
1396
1413
|
reportError(error, { type: "native", src }, getNativeVideoErrorMessage(error, messages));
|
|
1397
1414
|
}, [messages, reportError, videoSrc]);
|
|
1398
1415
|
const videoLoadedHandler = (0, import_react3.useCallback)(() => {
|
|
1399
1416
|
clearErrorState();
|
|
1400
|
-
|
|
1417
|
+
if (!pendingPlay.current || active === false || !canAttemptPlayback(videoRef.current)) return;
|
|
1418
|
+
void playAction();
|
|
1419
|
+
}, [active, clearErrorState, playAction]);
|
|
1401
1420
|
const mouseMoveHandler = (0, import_react3.useCallback)(() => {
|
|
1402
1421
|
if (controlTime.current) clearTimeout(controlTime.current);
|
|
1403
1422
|
if (!showControlRef.current) {
|
|
@@ -1427,7 +1446,6 @@ var VideoPlayerBase = (props, ref) => {
|
|
|
1427
1446
|
}, [muted]);
|
|
1428
1447
|
(0, import_react3.useEffect)(() => {
|
|
1429
1448
|
if (playingProp === void 0) return;
|
|
1430
|
-
if (!canAttemptPlayback(videoRef.current)) return;
|
|
1431
1449
|
if (playingProp) void playAction();
|
|
1432
1450
|
else pauseAction();
|
|
1433
1451
|
}, [pauseAction, playAction, playingProp]);
|
|
@@ -1572,6 +1590,7 @@ var VideoPlayerBase = (props, ref) => {
|
|
|
1572
1590
|
onPlay: () => setNextPlaying(true),
|
|
1573
1591
|
onPause: () => setNextPlaying(false),
|
|
1574
1592
|
onEnded: () => setNextPlaying(false),
|
|
1593
|
+
onLoadedMetadata: videoLoadedHandler,
|
|
1575
1594
|
onLoadedData: videoLoadedHandler,
|
|
1576
1595
|
onCanPlay: videoLoadedHandler,
|
|
1577
1596
|
onError: videoErrorHandler,
|
package/dist/index.js
CHANGED
|
@@ -833,6 +833,7 @@ var VideoPlayerBase = (props, ref) => {
|
|
|
833
833
|
const controlTime = useRef2(null);
|
|
834
834
|
const sourceLoadId = useRef2(0);
|
|
835
835
|
const sourceLoadMode = useRef2("native");
|
|
836
|
+
const pendingPlay = useRef2(false);
|
|
836
837
|
const [playingState, setPlayingState] = useState(!!defaultPlaying);
|
|
837
838
|
const [mutedState, setMutedState] = useState(!!defaultMuted);
|
|
838
839
|
const [durationState, setDurationState] = useState(0);
|
|
@@ -878,6 +879,7 @@ var VideoPlayerBase = (props, ref) => {
|
|
|
878
879
|
}, []);
|
|
879
880
|
const resetMediaForSourceChange = useCallback(() => {
|
|
880
881
|
clearTimers();
|
|
882
|
+
pendingPlay.current = false;
|
|
881
883
|
if (hlsRef.current) {
|
|
882
884
|
hlsRef.current.destroy();
|
|
883
885
|
hlsRef.current = null;
|
|
@@ -891,6 +893,7 @@ var VideoPlayerBase = (props, ref) => {
|
|
|
891
893
|
const releaseNativeMedia = useCallback(() => {
|
|
892
894
|
const video = videoRef.current;
|
|
893
895
|
if (!video) return;
|
|
896
|
+
pendingPlay.current = false;
|
|
894
897
|
video.pause();
|
|
895
898
|
if (sourceLoadMode.current !== "native" || !isVideoSourceAttached(video)) return;
|
|
896
899
|
video.removeAttribute("src");
|
|
@@ -898,9 +901,11 @@ var VideoPlayerBase = (props, ref) => {
|
|
|
898
901
|
}, []);
|
|
899
902
|
const ensureNativeMediaAttached = useCallback(() => {
|
|
900
903
|
const video = videoRef.current;
|
|
901
|
-
if (!video || sourceLoadMode.current !== "native" || isVideoSourceAttached(video)) return;
|
|
904
|
+
if (!video || sourceLoadMode.current !== "native" || isVideoSourceAttached(video)) return false;
|
|
902
905
|
video.src = videoSrc;
|
|
903
906
|
if (initialTime > 0) video.currentTime = initialTime;
|
|
907
|
+
video.load();
|
|
908
|
+
return true;
|
|
904
909
|
}, [initialTime, videoSrc]);
|
|
905
910
|
const setNextPlaying = useCallback(
|
|
906
911
|
(value) => {
|
|
@@ -1000,15 +1005,25 @@ var VideoPlayerBase = (props, ref) => {
|
|
|
1000
1005
|
const playAction = useCallback(async () => {
|
|
1001
1006
|
const el = videoRef.current;
|
|
1002
1007
|
if (!el) return;
|
|
1008
|
+
pendingPlay.current = true;
|
|
1003
1009
|
pauseOtherVideos(el);
|
|
1004
|
-
ensureNativeMediaAttached();
|
|
1010
|
+
const attachedNativeMedia = ensureNativeMediaAttached();
|
|
1011
|
+
if (sourceLoadMode.current === "native" && !canAttemptPlayback(el)) {
|
|
1012
|
+
if (attachedNativeMedia) clearErrorState();
|
|
1013
|
+
return;
|
|
1014
|
+
}
|
|
1005
1015
|
try {
|
|
1006
1016
|
await el.play();
|
|
1017
|
+
pendingPlay.current = false;
|
|
1007
1018
|
clearErrorState();
|
|
1008
1019
|
setNextPlaying(true);
|
|
1009
1020
|
notifyActiveChange(el, "play");
|
|
1010
1021
|
} catch (e) {
|
|
1011
|
-
if (isInterruptedPlayRequestError(e))
|
|
1022
|
+
if (isInterruptedPlayRequestError(e)) {
|
|
1023
|
+
pendingPlay.current = true;
|
|
1024
|
+
return;
|
|
1025
|
+
}
|
|
1026
|
+
pendingPlay.current = false;
|
|
1012
1027
|
console.debug(e);
|
|
1013
1028
|
reportError(e, { type: "play" }, messages.playFailed);
|
|
1014
1029
|
}
|
|
@@ -1036,6 +1051,7 @@ var VideoPlayerBase = (props, ref) => {
|
|
|
1036
1051
|
const pauseAction = useCallback(() => {
|
|
1037
1052
|
const el = videoRef.current;
|
|
1038
1053
|
if (!el) return;
|
|
1054
|
+
pendingPlay.current = false;
|
|
1039
1055
|
el.pause();
|
|
1040
1056
|
setNextPlaying(false);
|
|
1041
1057
|
notifyActiveChange(el, "pause");
|
|
@@ -1364,11 +1380,14 @@ var VideoPlayerBase = (props, ref) => {
|
|
|
1364
1380
|
const error = video == null ? void 0 : video.error;
|
|
1365
1381
|
const src = (video == null ? void 0 : video.currentSrc) || videoSrc;
|
|
1366
1382
|
if (hlsRef.current && src.startsWith("blob:")) return;
|
|
1383
|
+
pendingPlay.current = false;
|
|
1367
1384
|
reportError(error, { type: "native", src }, getNativeVideoErrorMessage(error, messages));
|
|
1368
1385
|
}, [messages, reportError, videoSrc]);
|
|
1369
1386
|
const videoLoadedHandler = useCallback(() => {
|
|
1370
1387
|
clearErrorState();
|
|
1371
|
-
|
|
1388
|
+
if (!pendingPlay.current || active === false || !canAttemptPlayback(videoRef.current)) return;
|
|
1389
|
+
void playAction();
|
|
1390
|
+
}, [active, clearErrorState, playAction]);
|
|
1372
1391
|
const mouseMoveHandler = useCallback(() => {
|
|
1373
1392
|
if (controlTime.current) clearTimeout(controlTime.current);
|
|
1374
1393
|
if (!showControlRef.current) {
|
|
@@ -1398,7 +1417,6 @@ var VideoPlayerBase = (props, ref) => {
|
|
|
1398
1417
|
}, [muted]);
|
|
1399
1418
|
useEffect(() => {
|
|
1400
1419
|
if (playingProp === void 0) return;
|
|
1401
|
-
if (!canAttemptPlayback(videoRef.current)) return;
|
|
1402
1420
|
if (playingProp) void playAction();
|
|
1403
1421
|
else pauseAction();
|
|
1404
1422
|
}, [pauseAction, playAction, playingProp]);
|
|
@@ -1543,6 +1561,7 @@ var VideoPlayerBase = (props, ref) => {
|
|
|
1543
1561
|
onPlay: () => setNextPlaying(true),
|
|
1544
1562
|
onPause: () => setNextPlaying(false),
|
|
1545
1563
|
onEnded: () => setNextPlaying(false),
|
|
1564
|
+
onLoadedMetadata: videoLoadedHandler,
|
|
1546
1565
|
onLoadedData: videoLoadedHandler,
|
|
1547
1566
|
onCanPlay: videoLoadedHandler,
|
|
1548
1567
|
onError: videoErrorHandler,
|