add volume,multiple

master
lgf 3 years ago
parent 3c93cc1056
commit 7ca6fd873c
  1. 27
      src/appComponent.tsx
  2. 12
      src/core/controller/index.tsx
  3. 4
      src/core/controls/index.tsx
  4. 30
      src/core/index.tsx
  5. 2
      src/core/progress/index.tsx
  6. 69
      src/core/useVideo.ts

@ -1,11 +1,34 @@
import React, { memo } from 'react'; import React, { memo, useRef, useEffect } from 'react';
import JoLPlayer from '@/core/index'; import JoLPlayer from '@/core/index';
import '@/icons/'; import '@/icons/';
import { useVideo } from '@/core/useVideo';
const AppComponent = memo(function AppComponent(props) { const AppComponent = memo(function AppComponent(props) {
const videoRef = useRef<HTMLVideoElement>(null!);
const xx = useRef(false);
useEffect(() => {}, [videoRef.current]);
const { isPlay, handleChangePlayState, currentTime } = useVideo({
videoElement:
videoRef.current &&
(videoRef.current as HTMLVideoElement & { video: HTMLVideoElement }).video,
// onPause: (val: any) => {
// console.log(val, xx.current);
// },
// onPlay: (val: any) => {
// console.log(val, xx.current);
// },
onTimeChange: (val: any) => {
// console.log(val);
},
// onEndEd: (val: any) => {
// console.log('结束', val);
// },
});
return ( return (
<> <>
<JoLPlayer /> {isPlay ? '播放' : '暂停'}
<JoLPlayer ref={videoRef} />
<JoLPlayer /> <JoLPlayer />
</> </>
); );

@ -16,20 +16,20 @@ const Index = memo(function Index(props) {
const { dispatch } = reviceProps; const { dispatch } = reviceProps;
const { isPlay, handleChangePlayState, currentTime } = useVideo({ const { isPlay, handleChangePlayState, currentTime } = useVideo({
videoElement: reviceProps.videoRef,
onPause: (val: any) => { onPause: (val: any) => {
console.log(val); console.log(val);
}, },
onPlay: (val: any) => { onPlay: (val: any) => {
console.log(val); console.log(val);
}, },
onTimeChange: (val: any) => { // onTimeChange: (val: any) => {
// console.log(val); // console.log(val);
}, // },
onEndEd: (val: any) => { // onEndEd: (val: any) => {
console.log('结束', val); // console.log('结束', val);
}, // },
}); });
const timer = useRef<NodeJS.Timeout | null>(null!); const timer = useRef<NodeJS.Timeout | null>(null!);
const viewClientX = useRef<number>(null!); const viewClientX = useRef<number>(null!);

@ -30,7 +30,9 @@ const Index = memo(function Index(props) {
const revicePropsData = useRef<any>(); const revicePropsData = useRef<any>();
const { isPlay, handleChangePlayState, currentTime, duration, isPictureinpicture } = useVideo({}); const { isPlay, handleChangePlayState, currentTime, duration, isPictureinpicture } = useVideo({
videoElement: reviceProps.videoRef,
});
const { controlsState, dispatch } = useControls(); const { controlsState, dispatch } = useControls();

@ -1,4 +1,11 @@
import React, { memo, useRef, useState, useEffect, useMemo } from 'react'; import React, {
forwardRef,
useRef,
useState,
useEffect,
useMemo,
useImperativeHandle,
} from 'react';
import Controller from './controller'; import Controller from './controller';
import { videoparameter } from '@/interface'; import { videoparameter } from '@/interface';
import { FlowContext, useVideoFlow } from '@/core/context'; import { FlowContext, useVideoFlow } from '@/core/context';
@ -8,11 +15,14 @@ import Broadcast from '@/components/svgIcon';
import '@/assets/css/reset.scss'; import '@/assets/css/reset.scss';
import './index.scss'; import './index.scss';
const Index = memo(function Index({ const JoLPlayer = function JoLPlayer(
{
videoSrc = 'https://gs-files.oss-cn-hongkong.aliyuncs.com/okr/test/file/2021/07/01/haiwang.mp4', videoSrc = 'https://gs-files.oss-cn-hongkong.aliyuncs.com/okr/test/file/2021/07/01/haiwang.mp4',
}: { }: {
videoSrc?: string; videoSrc?: string;
}) { },
ref: React.Ref<unknown> | undefined,
) {
/** /**
* @description * @description
*/ */
@ -50,6 +60,10 @@ const Index = memo(function Index({
setIsBufferring(false); setIsBufferring(false);
}; };
useImperativeHandle(ref, () => ({
video: videoRef.current,
}));
useEffect(() => { useEffect(() => {
/** /**
* @description * @description
@ -60,7 +74,6 @@ const Index = memo(function Index({
timerToCheckVideoUseful.current = setTimeout(() => { timerToCheckVideoUseful.current = setTimeout(() => {
// 当视频未初始化时(即不可用时) // 当视频未初始化时(即不可用时)
if (videoElem.networkState === 0) { if (videoElem.networkState === 0) {
console.log('can not find');
setIsVideoUseful(false); setIsVideoUseful(false);
} else { } else {
clearTimeout(timerToCheckVideoUseful.current!); clearTimeout(timerToCheckVideoUseful.current!);
@ -79,7 +92,7 @@ const Index = memo(function Index({
videoElem.removeEventListener('waiting', waitingListener); videoElem.removeEventListener('waiting', waitingListener);
videoElem.removeEventListener('playing', playingListener); videoElem.removeEventListener('playing', playingListener);
}; };
}, []); }, [videoRef.current]);
const returnVideoSource = useMemo(() => { const returnVideoSource = useMemo(() => {
return ( return (
@ -119,6 +132,7 @@ const Index = memo(function Index({
</FlowContext.Provider> </FlowContext.Provider>
</figure> </figure>
); );
}); };
export default Index; const JoLPlayerComponent = forwardRef<HTMLVideoElement, any>(JoLPlayer);
export default JoLPlayerComponent;

@ -29,7 +29,7 @@ const Index = memo(function Index(props) {
const reviceProps = useContext(FlowContext); const reviceProps = useContext(FlowContext);
const { currentTime, duration, bufferedTime } = useVideo({}); const { currentTime, duration, bufferedTime } = useVideo({ videoElement: reviceProps.videoRef });
const { progressState, dispatch } = useProgress(); const { progressState, dispatch } = useProgress();

@ -2,7 +2,10 @@ import { useRef, useMemo, useEffect, useState, useReducer, useContext, useCallba
import useMandatoryUpdate from '@/utils/useMandatoryUpdate'; import useMandatoryUpdate from '@/utils/useMandatoryUpdate';
import { FlowContext } from '@/core/context'; import { FlowContext } from '@/core/context';
export const useVideo = ({ onPause, onPlay, onTimeChange, onEndEd }: any) => { export const useVideo = (
{ videoElement, onPause, onPlay, onTimeChange, onEndEd }: any,
dep?: any,
) => {
const forceUpdate = useMandatoryUpdate(); const forceUpdate = useMandatoryUpdate();
const reviceProps = useContext(FlowContext); const reviceProps = useContext(FlowContext);
@ -15,53 +18,44 @@ export const useVideo = ({ onPause, onPlay, onTimeChange, onEndEd }: any) => {
duration: 0, duration: 0,
bufferedTime: 0, bufferedTime: 0,
isPictureinpicture: false, isPictureinpicture: false,
volume: 0,
multiple: 1.0,
}); });
const videoRef = useRef<HTMLVideoElement | null>(null); const videoRef = useRef<HTMLVideoElement>(null!);
const interval = useRef<NodeJS.Timeout | null>(null!); const interval = useRef<NodeJS.Timeout | null>(null!);
videoRef.current = videoEle; videoRef.current = videoElement;
useEffect(() => { useEffect(() => {
forceUpdate();
if (videoRef.current) { if (videoRef.current) {
/** /**
* @description * @description
*/ */
videoRef.current.addEventListener('canplay', () => { videoRef.current.addEventListener('canplay', () => {
videoParameter.current = { torture({ duration: videoRef.current.duration });
...videoParameter.current,
duration: videoRef.current!.duration,
};
}); });
/** /**
* @description * @description
*/ */
videoRef.current.addEventListener('progress', () => { videoRef.current.addEventListener('progress', () => {
if (videoRef.current!.buffered.length >= 1) { if (videoRef.current.buffered.length >= 1) {
videoParameter.current = { torture({ bufferedTime: videoRef.current.buffered.end(0) });
...videoParameter.current,
bufferedTime: videoRef.current!.buffered.end(0),
};
} }
}); });
/** /**
* @description * @description
*/ */
videoRef.current.addEventListener('enterpictureinpicture', () => { videoRef.current.addEventListener('enterpictureinpicture', () => {
videoParameter.current = { torture({ isPictureinpicture: true });
...videoParameter.current,
isPictureinpicture: true,
};
}); });
/** /**
* @description 退 * @description 退
*/ */
videoRef.current.addEventListener('leavepictureinpicture', () => { videoRef.current.addEventListener('leavepictureinpicture', () => {
videoParameter.current = { torture({ isPictureinpicture: false });
...videoParameter.current,
isPictureinpicture: false,
};
}); });
// 定时器用于更新当前视频时间 // 定时器用于更新当前视频时间
interval.current = setInterval(() => { interval.current = setInterval(() => {
@ -69,11 +63,13 @@ export const useVideo = ({ onPause, onPlay, onTimeChange, onEndEd }: any) => {
* *
*/ */
forceUpdate(); forceUpdate();
videoParameter.current = {
...videoParameter.current, torture({
currentTime: videoRef.current!.currentTime, currentTime: videoRef.current.currentTime,
isPlay: videoRef.current!.paused ? false : true, isPlay: videoRef.current.paused ? false : true,
}; volume: videoRef.current.volume,
multiple: videoRef.current.playbackRate,
});
}, 1); }, 1);
videoRef.current.addEventListener('pause', pauseChange); videoRef.current.addEventListener('pause', pauseChange);
videoRef.current.addEventListener('play', playChange); videoRef.current.addEventListener('play', playChange);
@ -83,15 +79,21 @@ export const useVideo = ({ onPause, onPlay, onTimeChange, onEndEd }: any) => {
return () => { return () => {
interval.current && clearInterval(interval.current); interval.current && clearInterval(interval.current);
}; };
}, [videoEle, videoRef.current]); }, [videoRef.current, dep, videoElement]);
const torture = (val: any) => {
videoParameter.current = { ...videoParameter.current, ...val };
};
const pauseChange = () => { const pauseChange = () => {
torture({ isPlay: videoRef.current.paused ? false : true });
onPause && onPause(videoParameter.current); onPause && onPause(videoParameter.current);
}; };
const playChange = () => { const playChange = () => {
torture({ isPlay: videoRef.current.paused ? false : true });
onPlay && onPlay(videoParameter.current); onPlay && onPlay(videoParameter.current);
}; };
const timeupdate = () => { const timeupdate = () => {
torture({ isPlay: videoRef.current.paused ? false : true });
onTimeChange && onTimeChange(videoParameter.current); onTimeChange && onTimeChange(videoParameter.current);
}; };
const endedChange = () => { const endedChange = () => {
@ -99,17 +101,16 @@ export const useVideo = ({ onPause, onPlay, onTimeChange, onEndEd }: any) => {
}; };
const handleChangePlayState = () => { const handleChangePlayState = () => {
if (videoParameter.current.isPlay) { if (videoParameter.current.isPlay) {
videoRef.current!.pause(); videoRef.current.pause();
} else { } else {
videoRef.current!.play(); videoRef.current.play();
} }
videoParameter.current = {
...videoParameter.current,
isPlay: videoRef.current!.paused ? false : true,
}; };
}; return useMemo(
return { () => ({
handleChangePlayState, handleChangePlayState,
...videoParameter.current, ...videoParameter.current,
}; }),
[videoParameter.current],
);
}; };

Loading…
Cancel
Save