-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathPlayer.tsx
More file actions
115 lines (97 loc) · 2.87 KB
/
Player.tsx
File metadata and controls
115 lines (97 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import React, { useCallback, useEffect, useRef } from 'react';
import type { SyntheticEvent } from 'react';
import type { PlayerEntry } from './players.js';
import type { ReactPlayerProps } from './types.js';
type Player = React.ForwardRefExoticComponent<
ReactPlayerProps & {
activePlayer: PlayerEntry['player'];
}
>;
const Player: Player = React.forwardRef((props, ref) => {
const { playing, pip } = props;
const Player = props.activePlayer;
const playerRef = useRef<HTMLVideoElement | null>(null);
const startOnPlayRef = useRef(true);
useEffect(() => {
if (!playerRef.current) return;
// Use strict equality for `playing`, if it's nullish, don't do anything.
if (playerRef.current.paused && playing === true) {
playerRef.current.play();
}
if (!playerRef.current.paused && playing === false) {
playerRef.current.pause();
}
playerRef.current.playbackRate = props.playbackRate ?? 1;
playerRef.current.volume = props.volume ?? 1;
});
useEffect(() => {
if (!playerRef.current || !globalThis.document) return;
if (pip && !document.pictureInPictureElement) {
try {
playerRef.current.requestPictureInPicture?.();
} catch (err) {}
}
if (!pip && document.pictureInPictureElement) {
try {
// @ts-ignore
playerRef.current.exitPictureInPicture?.();
document.exitPictureInPicture?.();
} catch (err) {}
}
}, [pip]);
const handleLoadStart = (event: SyntheticEvent<HTMLVideoElement>) => {
startOnPlayRef.current = true;
props.onReady?.();
props.onLoadStart?.(event);
};
const handlePlay = (event: SyntheticEvent<HTMLVideoElement>) => {
if (startOnPlayRef.current) {
startOnPlayRef.current = false;
props.onStart?.(event);
}
props.onPlay?.(event);
};
if (!Player) {
return null;
}
const eventProps: Record<string, EventListenerOrEventListenerObject> = {};
for (const key in props) {
if (key.startsWith('on')) {
eventProps[key] = props[key as keyof ReactPlayerProps];
}
}
return (
<Player
{...eventProps}
style={props.style}
className={props.className}
slot={props.slot}
ref={useCallback(
(node: HTMLVideoElement) => {
playerRef.current = node;
if (typeof ref === 'function') {
ref(node);
} else if (ref !== null) {
ref.current = node;
}
},
[ref]
)}
src={props.src}
crossOrigin={props.crossOrigin}
preload={props.preload}
controls={props.controls}
muted={props.muted}
autoPlay={props.autoPlay}
loop={props.loop}
playsInline={props.playsInline}
config={props.config}
onLoadStart={handleLoadStart}
onPlay={handlePlay}
>
{props.children}
</Player>
);
});
Player.displayName = 'Player';
export default Player;