import React, { useState, useRef, useEffect } from 'react';
import PlayIcon from '@/assets/play.png';
import VideoLoadIcon from '../../assets/03.gif';
import './index.less';
const VideoPlay = ({ item }) => {
const timer = useRef();
const [showVideo,setShowVideo] = useState(false);
const imgId = item.id+'-img';
useEffect(() => {
if(window.WeixinJSBridge){
WeixinJSBridge.invoke('getNetworkType', {}, function(e) {
WeixinJSBridge.invoke('playVideo', {
'url': item.src || item.attributes.src,
}, function(res) {
const video = document.getElementById(item.id);
video.pause();
video.muted = false;
});
});
}
return() => {
if(timer.current){
clearTimeout(timer.current);
}
}
},[]);
const handleClick = (id,imgId) => {
const myVideo = document.getElementById(id);
try {
if(myVideo){
myVideo?.play();
myVideo.controls = true;
}
} catch (error) {
console.log(error)
}
}
const pauseVideo = (id,imgId,style,controls) => {
const imgEle = document.getElementById(imgId);
try {
imgEle.classList.add('hidden');
} catch (error) {
console.log(error)
}
}
const handleLoadComplete = () => {
let video = document.getElementById(item.id);
video.currentTime = 0.2;
setShowVideo(true);
}
return(
<>
<div key={item.id} className='video-container' >
<video
disablePictureInPicture
controlsList="nodownload noremoteplayback "
id={item.id}
onPlay={() => pauseVideo(item.id,imgId,'none',true)}
onPause={() => pauseVideo(item.id,imgId,'inline-block',false)}
preload="metadata"
muted={true}
onLoadedMetadata={handleLoadComplete}
>
<source src={(item.src || item.attributes.src)} ></source>
</video>
<img className={`video-play ${showVideo?'':'hidden'}`} id={imgId} onClick={() => handleClick(item.id,imgId)} alt='播放' src={PlayIcon} />
<div className={`load-vide-con ${showVideo?'hidden':''}`} >
<img alt='' src={VideoLoadIcon} />
<span>加载中</span>
</div>
</div>
</>
)
}
export default VideoPlay;
.video-container{
position: relative;
display: inline-block;
width: 320px;
.video-play{
width: 40px;
height: 40px;
position: absolute;
top:50%;
left: 50%;
transform: translate(-50%,-50%);
cursor: pointer;
}
.load-vide-con{
background-color: rgba(0,0,0,0.08);
border-radius: 16px;
width: 320px;
height: 180px;
position: absolute;
top: 0px;
display: flex;
flex-direction: column;
justify-content: center;
align-items: center;
img{
width: 34px;
height: 34px;
margin-bottom: 4px;
}
span{
color: rgba(0,0,0,0.46);
font-size: 12px;
}
}
.hidden{
display: none;
}
}
.video-progress{
height: 20px;
width: 320px;
}
video::-webkit-media-controls-volume-control-container {
display: none !important;
}
*::-webkit-media-controls-start-playback-button {
display: none ;
-webkit-appearance: none;
}