我的任务是通过一些动作在videosphere中播放和停止视频。通过单击videosphere中的哪个视频应该更改,我可以获得元素列表。我正在使用React.js和一个框架库。 我花了很多时间来实现所需的行为,因此我想分享一些代码和注释。突然有人会有所帮助。
答案 0 :(得分:0)
一些重要的事情:
我的代码片段:
static getDerivedStateFromProps(nextProps, prevState){
let update = {};
if(nextProps.scene_item){
let isVideoSphere = nextProps.scene_item.type_id === experienceTypes.videosphere;
update.sceneUrl = nextProps.scene_item.asset_url;
update.isVideoSphere = isVideoSphere;
if(isVideoSphere){
playVideo('scene-video',true)
}
}
return Object.keys(update).length ? update : null;
}
componentWillUnmount() {
pauseVideo('scene-video')
}
export function pauseVideo(id) {
//pause aframe 360 video
let video = document.getElementById(id);
video.currentTime = 0;
video.pause()
}
export function playVideo(id,first_playing) {
//play aframe 360 video
let video = document.getElementById(id);
let el = document.getElementById('clikable-el-for-play-video');
if(video){
if(first_playing){
if(el){
el.click()
video.play()
}
}else video.play()
}
}
<a-scene>
<a-assets>
<video id="scene-video" src={sceneUrl} loop/>
</a-assets>
<a-videosphere src={'#scene-video'}/>
<span className="hidden" id="clikable-el-for-play-video"/>
</a-scene>
在safari中,发出点击事件无效,因此我进行了更改:
handleClickOnScene = (event) => {
let videoId = `scene-video-${this.props.scene_item.id}`;
let video = document.getElementById(videoId);
if(video.muted && !video.paused){
setTimeout(increaseVideoVolume(videoId), 5000)
}
}
export function increaseVideoVolume(id,first_playing) {
let video = document.getElementById(id);
if(video){
if(video.muted){
video.muted = false;
}
}
}