|
| 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; |
0 commit comments