[英]video doesn't autoplay after use react-snap
I have a video run in the background in my react app.我的 React 应用程序在后台运行了一段视频。 It works perfectly and auto-plays on page load.
它完美运行并在页面加载时自动播放。
here is the code JSX I have used.这是我使用的代码 JSX。
<video
autoPlay
loop
style={{
backgroundImage: `url(${topVideoImage})`,
}}
muted
playsInline
>
<source type="video/mp4" src={topVideo} />
</video>
It doesn't auto play after I use react-snap
.我使用
react-snap
后它不会自动播放。 my package.json looks like this:我的 package.json 看起来像这样:
"scripts": {
"start": "react-scripts start",
"build": "react-scripts build",
"postbuild": "react-snap",
"test": "react-scripts test",
"eject": "react-scripts eject"
},
It works after I remove "postbuild": "react-snap",
.它在我删除
"postbuild": "react-snap",
。 How can I solve (autoplay the video on load) this without removing react-snap
?如何在不删除
react-snap
情况下解决(加载时自动播放视频)?
I found a solution to this problem.我找到了解决这个问题的方法。 According to this muted is now required for chrome to autoplay the video on load.
根据这个静音现在需要的铬自动播放在加载视频。 We can mute video using
muted
attribute But react-snap
is not processing again now.我们可以使用
muted
属性静音视频但是react-snap
现在不再处理了。 So we have to manually set muted
attribute to true
.所以我们必须手动将
muted
属性设置为true
。
For that, I have used this simple hack.为此,我使用了这个简单的 hack。
const makeMuted = (elt) => {
if (elt) {
elt.muted = true;
}
};
export const LandingPage = (props) => {
return ( <video
id="background-video"
autoPlay
loop
ref={makeMuted}
playsInline
>
<source type="video/mp4" src={topVideo} />
</video>)
}
I ran into this same issue even when explicitly setting muted
to true in JSX as well as programmatically using Javascript.即使在 JSX 中将
muted
显式设置为 true 以及以编程方式使用 Javascript 时,我也遇到了同样的问题。
Instead of using the autoPlay
attribute, consider combining the .play()
method with either or both of the following.不要使用
autoPlay
属性,而是考虑将.play()
方法与以下任一或两者结合使用。
canplay
event canplay
活动The video's canPlay
event will fire off once enough of the video has been downloaded to begin playback without likely encountering any buffering issues.一旦下载了足够的视频开始播放,视频的
canPlay
事件就会触发,而不会遇到任何缓冲问题。
elt.addEventListener("canplay", () => {
elt.play();
})
readyState
attribute readyState
属性The video's readyState
attribute will have a value of 4 if enough of the video has been downloaded to begin playback without likely encountering any buffering issues.如果已下载足够的视频以开始播放,而不会遇到任何缓冲问题,则视频的
readyState
属性的值为 4。
if(elt.readyState > 4){
elt.play()
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.