Skip to content

Commit e7670af

Browse files
committed
Add video.js/demo
1 parent 00450db commit e7670af

File tree

8 files changed

+267
-9
lines changed

8 files changed

+267
-9
lines changed

package.json

+3-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,9 @@
4141
"umi": "^3.2.5",
4242
"pdfjs-dist": "1.6.210",
4343
"use-deep-compare-effect": "^1.6.1",
44-
"yorkie": "^2.0.0"
44+
"yorkie": "^2.0.0",
45+
"videojs": "^1.0.0",
46+
"videojs-contrib-hls": "5.15.0"
4547
},
4648
"devDependencies": {
4749
"babel-eslint": "^10.0.1",

src/components/Vjs/VideoH5.jsx

+111
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
import { useRef } from 'react';
2+
import Video from './Vjs';
3+
4+
const VideoH5 = props => {
5+
const { onReady, style } = props;
6+
7+
const playerRef = useRef(null);
8+
9+
/**
10+
* 播放视频
11+
*/
12+
const play = (source, opts) => {
13+
const player = playerRef.current;
14+
if (player) {
15+
const d = source?.src;
16+
if (d) {
17+
if (opts?.currentTime) {
18+
const handleLoaded = function() {
19+
window.console.log('loadedmetadata');
20+
player.currentTime(opts?.currentTime);
21+
};
22+
// watch once
23+
player.one('loadedmetadata', handleLoaded);
24+
}
25+
26+
player.src(source);
27+
player.load(source);
28+
}
29+
}
30+
};
31+
32+
/**
33+
* 获取当前的播放位置
34+
*/
35+
const getDuration = () => {
36+
const player = playerRef.current;
37+
if (player) {
38+
return player.currentTime();
39+
}
40+
return 0;
41+
};
42+
43+
/**
44+
* 禁止拖动播放条
45+
*/
46+
const disableProgress = disabled => {
47+
const player = playerRef.current;
48+
if (player) {
49+
player.disableProgress({ autoDisable: disabled });
50+
}
51+
};
52+
53+
const handleOnTimeUpdate = function() {
54+
const { onTimeUpdate } = props;
55+
const duration = this.duration();
56+
const currentTime = this.currentTime();
57+
58+
if (onTimeUpdate instanceof Function) {
59+
onTimeUpdate?.({ duration, currentTime });
60+
}
61+
};
62+
63+
const onVjsLoaded = player => {
64+
playerRef.current = player;
65+
66+
player.on('timeupdate', handleOnTimeUpdate);
67+
68+
69+
if (onReady instanceof Function) {
70+
onReady({ play, getDuration, disableProgress });
71+
}
72+
};
73+
74+
return (
75+
<Video
76+
style={style}
77+
options={{
78+
autoplay: false,
79+
controls: true,
80+
muted: false,
81+
fluid: true,
82+
// poster: 'xxx', // 视频封面图地址
83+
controlBar: {
84+
children: [
85+
{ name: 'playToggle' }, // 播放按钮
86+
{ name: 'currentTimeDisplay' }, // 当前已播放时间
87+
{ name: 'PlayNext' }, //点击播放下一个按钮
88+
{ name: 'progressControl' }, // 播放进度条
89+
{ name: 'durationDisplay' }, // 总时间
90+
{ name: 'remainingTimeDisplay' }, // 剩下的时间
91+
{
92+
name: 'playbackRateMenuButton',
93+
playbackRates: [0.5, 1, 1.5, 2]
94+
}, // 倍数播放
95+
{
96+
name: 'volumePanel', // 音量控制
97+
inline: false // 不使用水平方式
98+
},
99+
{ name: 'FullscreenToggle' } // 全屏
100+
]
101+
},
102+
preload: true,
103+
responsive: true,
104+
sources: []
105+
}}
106+
onVjsLoaded={onVjsLoaded}
107+
/>
108+
);
109+
};
110+
111+
export default VideoH5;

src/components/Vjs/Vjs.jsx

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { useRef, useEffect } from 'react';
2+
import videojs from 'video.js';
3+
4+
window.videojs = videojs;
5+
6+
require('video.js/dist/video-js.css');
7+
require('videojs-contrib-hls/dist/videojs-contrib-hls');
8+
require('./plugins/disableProgress');
9+
require('./Vjs.less');
10+
11+
const Video = props => {
12+
const videoRef = useRef(null);
13+
const playerRef = useRef(null);
14+
const { options, onVjsLoaded, style } = props;
15+
16+
useEffect(() => {
17+
// make sure Video.js player is only initialized once
18+
if (!playerRef.current) {
19+
const videoElement = videoRef.current;
20+
if (!videoElement) return;
21+
22+
playerRef.current = videojs(videoElement, options, () => {
23+
onVjsLoaded?.(playerRef.current);
24+
});
25+
}
26+
return () => {
27+
const player = playerRef.current;
28+
if (player) {
29+
player.dispose();
30+
playerRef.current = null;
31+
}
32+
};
33+
}, [videoRef]);
34+
35+
return (
36+
<div
37+
style={{ width: style?.width ?? '100%', height: style?.height ?? 210 }}
38+
>
39+
<video
40+
ref={videoRef}
41+
className='video-js vjs-big-play-centered'
42+
playsInline
43+
>
44+
<p className='vjs-no-js'>当前浏览器不支持 Video</p>
45+
</video>
46+
</div>
47+
);
48+
};
49+
50+
export default Video;

src/components/Vjs/Vjs.less

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
.video-js button {
2+
outline: none;
3+
}
4+
.video-js.vjs-fluid,
5+
.video-js.vjs-16-9,
6+
.video-js.vjs-4-3,
7+
.video-js.vjs-tech {
8+
height: 100%;
9+
background-color: #161616;
10+
}
11+
12+
.video-js.vjs-fluid {
13+
padding-top: 0;
14+
}
15+
16+
.vjs-poster {
17+
background-color: #161616;
18+
}
19+
.video-js.vjs-error .vjs-big-play-button {
20+
display: none;
21+
}
22+
.vjs-paused .vjs-big-play-button {
23+
display: block;
24+
}
25+
.video-js .vjs-big-play-button {
26+
border: none;
27+
}
28+
29+
// controls
30+
31+
.vjs-remaining-time {
32+
display: block !important;
33+
}
34+
35+
.vjs-playback-rate {
36+
display: flex !important;
37+
}
38+
39+
.vjs-playback-rate-value {
40+
font-size: 1.4em !important;
41+
}
42+
43+
.vjs-menu-item-text {
44+
font-size: 1.3em;
45+
}

src/components/Vjs/index.js

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import VideoH5 from './VideoH5';
2+
3+
export default VideoH5;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/**
2+
* videojs的插件
3+
* 用于禁止拖拽进度条
4+
*/
5+
(function(vjs) {
6+
var extend = function(obj /*, arg1, arg2, ... */) {
7+
var arg, i, k;
8+
for (i = 1; i < arguments.length; i++) {
9+
arg = arguments[i];
10+
for (k in arg) {
11+
if (arg.hasOwnProperty(k)) {
12+
obj[k] = arg[k];
13+
}
14+
}
15+
}
16+
return obj;
17+
},
18+
defaults = {
19+
autoDisable: false
20+
},
21+
disableProgress = function(options) {
22+
var player = this,
23+
state = false,
24+
settings = extend({}, defaults, options || {});
25+
26+
player.controlProgress = {
27+
disable: function() {
28+
state = true;
29+
30+
const controls = document.querySelector('.vjs-progress-control');
31+
if (controls) {
32+
controls.style.pointerEvents = 'none';
33+
}
34+
},
35+
enable: function() {
36+
state = false;
37+
const controls = document.querySelector('.vjs-progress-control');
38+
if (controls) {
39+
controls.style.pointerEvents = 'auto';
40+
}
41+
},
42+
getState: function() {
43+
return state;
44+
}
45+
};
46+
47+
if (settings.autoDisable) {
48+
player.controlProgress.disable();
49+
}
50+
};
51+
52+
vjs.plugin('disableProgress', disableProgress);
53+
})(window.videojs);

src/hooks/useDraggable.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
1-
import { useRef } from 'react';
1+
import { useRef, useEffect } from 'react';
22
import { off, on } from 'utils/dom';
3-
import useMount from './useMount';
43

54
/**
65
* @hook useDraggable
7-
* @desc 使得DOM变得可拖拽
8-
* @at 2020/09/22
9-
* @by lmh
106
* */
117

128
// 拖拽的初始位置
@@ -25,7 +21,7 @@ const useDraggable = (
2521
const isDragging = useRef(null);
2622
const ref = useRef(null);
2723

28-
useMount(() => {
24+
useEffect(() => {
2925
const mouseMove = e => {
3026
if (ref.current === currentTarget) {
3127
if (isDragging.current) {
@@ -36,7 +32,6 @@ const useDraggable = (
3632

3733
let x = e.clientX - initPosition.x;
3834
let y = e.clientY - initPosition.y;
39-
4035
// 是否允许 拖拽位置脱离边界
4136
if (!overbound) {
4237
if (x < 0) x = 0;

src/pages/hook/examples/useDraggable.js

-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import React, { useRef, useState } from 'react';
33
import Wrapper from 'components/Wrapper';
44
import Description from 'components/Description';
55
import useDraggable from 'hooks/useDraggable';
6-
import Links from 'components/Links';
76
import Part from 'components/Part';
87
import DraggleLayout from 'components/DraggleLayout';
98

0 commit comments

Comments
 (0)