File size: 7,512 Bytes
1e92f2d |
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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 |
import React, {
forwardRef,
useRef,
useState,
useEffect,
useMemo,
useImperativeHandle,
useCallback,
} from 'react';
import Controller from './controller';
// import { videoparameter, JoLPlayerRef } from '@/interface';
import { FlowContext, useVideoFlow } from '@/core/context';
import useMandatoryUpdate from '@/utils/useMandatoryUpdate';
import BufferComponent from '@/components/svgIcon';
import { useVideo } from '@/core/useVideo';
import useVideoCallback from '@/core/useVideoCallback';
import { defaultTheme } from '@/core/config';
import Hls from 'hls.js';
import toast from '@/components/toast';
import '@/assets/css/reset.scss';
import './index.scss';
import { JoLPlayerRef, videoparameter } from 'types';
const JoLPlayer = function JoLPlayer(props: videoparameter, ref: React.Ref<unknown> | undefined) {
const {
option,
className,
style,
onProgressMouseDown,
onPlay,
onPause,
onTimeChange,
onEndEd,
onProgressMouseUp,
onError,
onvolumechange,
onQualityChange,
} = props;
const {
videoSrc,
width,
height,
theme,
poster,
setBufferContent,
videoType,
toastPosition,
autoPlay,
mode,
} = option;
/**
* @description 关灯对象
*/
const lightOffMaskRef = useRef<HTMLDivElement>(null!);
/**
* @description 视频对象
*/
const videoRef = useRef<HTMLVideoElement>(null!);
/**
* @description 视频容器对象
*/
const videoContainerRef = useRef<HTMLDivElement>(null!);
/**
* @description 定时器检测 3 秒后视频是否可用
*/
const timerToCheckVideoUseful = useRef<NodeJS.Timeout | null>(null);
/**
* @description 视频缓存的开关
*/
const [isBufferring, setIsBufferring] = useState<boolean>(false);
/**
* @description 用来处理视频自动播放的
*/
const muted = useRef<boolean>(false);
const { videoFlow, dispatch } = useVideoFlow();
const forceUpdate = useMandatoryUpdate();
const waitingListener = () => {
setIsBufferring(true);
};
const playingListener = () => {
setIsBufferring(false);
};
const setVideoWh = (e: Event) => {
const event = e.target as EventTarget & { videoWidth: number; videoHeight: number };
const videoWidth = event.videoWidth;
const videoHeight = event.videoHeight;
if (width && height) {
videoContainerRef.current.style.width = `${width}px`;
videoContainerRef.current.style.height = `${height}px`;
} else {
if (mode === 'widthFix' && width) {
const scaleH = (width * videoHeight) / videoWidth;
videoContainerRef.current.style.height = `${scaleH}px`;
videoContainerRef.current.style.width = `${width}px`;
} else if (mode === 'heightFix' && height) {
const scaleW = (videoWidth * height) / videoHeight;
videoContainerRef.current.style.width = `${scaleW}px`;
videoContainerRef.current.style.height = `${height}px`;
} else {
videoContainerRef.current.style.width = `${width ? width : videoWidth}px`;
videoContainerRef.current.style.height = `${height ? height : videoHeight}px`;
}
}
};
const setHls = (videoElem: HTMLVideoElement) => {
if (videoType && videoType === 'hls') {
// 支持hls格式
if (Hls.isSupported()) {
var hls = new Hls();
hls.loadSource(videoSrc);
hls.attachMedia(videoElem);
}
}
};
const { videoAttributes, videoMethod } = useVideo(
{
videoElement: videoRef.current,
},
[videoRef.current],
);
useVideoCallback(videoAttributes, videoFlow, {
onProgressMouseDown,
onProgressMouseUp,
onPlay,
onPause,
onTimeChange,
onEndEd,
onError,
onvolumechange,
onQualityChange,
});
useEffect(() => {
forceUpdate();
const videoElem = videoRef.current;
// setVideoContainerStyle(videoContainerRef.current, height, width);
setHls(videoElem);
if (option.autoPlay) {
muted.current = true;
videoRef.current.volume = 0;
}
timerToCheckVideoUseful.current = setTimeout(() => {
// 当视频未初始化时(即不可用时)
if (videoElem.networkState === 0 || videoElem.networkState === 3) {
toast({
message: 'Error:Video source not found',
duration: 4000,
position: toastPosition,
});
} else {
clearTimeout(timerToCheckVideoUseful.current!);
}
}, 3000);
videoElem.addEventListener('canplay', setVideoWh);
videoElem.addEventListener('waiting', waitingListener);
videoElem.addEventListener('playing', playingListener);
return () => {
timerToCheckVideoUseful.current && clearTimeout(timerToCheckVideoUseful.current);
videoElem.removeEventListener('waiting', waitingListener);
videoElem.removeEventListener('playing', playingListener);
};
}, [option]);
useEffect(() => {
if (autoPlay) {
document.addEventListener('mousemove', () => videoAutoPlay(autoPlay));
}
return () => {
autoPlay && document.removeEventListener('mousemove', () => videoAutoPlay);
};
}, [autoPlay]);
const videoAutoPlay = useCallback((autoPlay: boolean) => {
if (!autoPlay) {
return;
}
if (muted.current && videoRef.current) {
var promise = videoRef.current.play();
if (promise !== undefined) {
promise
.catch((error) => {
videoRef.current.volume = 0.6;
muted.current = false;
})
.then(() => {
videoRef.current.play();
videoRef.current.volume = 0.6;
muted.current = false;
});
}
} else {
return null;
}
}, []);
useImperativeHandle(ref, () => {
return {
video: videoRef.current,
...videoMethod,
...videoAttributes,
};
});
const returnVideoSource = useMemo(() => {
return (
<>
<source src={videoSrc} type="video/mp4" />
<source src={videoSrc} type="video/ogg" />
<source src={videoSrc} type="video/webm" />
</>
);
}, [videoSrc]);
const contextProps = useMemo(() => {
return Object.assign(
{},
{
videoRef: videoRef.current,
videoContainerRef: videoContainerRef.current,
lightOffMaskRef: lightOffMaskRef.current,
dispatch,
videoFlow,
propsAttributes: option,
},
);
}, [videoRef.current, videoFlow, option]);
return (
<div
className={`JoL-player-container ${className}`}
ref={videoContainerRef}
style={style}
id="JoL-player-container"
>
<div className="JoL-light-off-mask" ref={lightOffMaskRef}></div>
<video
autoPlay={autoPlay}
muted={muted.current}
className="JoL-player"
ref={videoRef}
src={videoSrc}
poster={poster ? poster : undefined}
id="JoL-player"
>
{returnVideoSource}
</video>
{isBufferring &&
(setBufferContent ? (
setBufferContent
) : (
<BufferComponent
iconClass="loading"
fill={theme ? theme : defaultTheme}
className="player-loading"
fontSize="55px"
/>
))}
<FlowContext.Provider value={contextProps}>
<Controller />
</FlowContext.Provider>
</div>
);
};
const JoLPlayerComponent = forwardRef<JoLPlayerRef, videoparameter>(JoLPlayer);
export default JoLPlayerComponent;
|