Skip to content
Snippets Groups Projects
Commit 66be4349 authored by Christopher Van Wiemeersch's avatar Christopher Van Wiemeersch
Browse files

handle playback of background video when page loses focus (fixes issue #310)

parent 0cdf8bf0
No related branches found
No related tags found
No related merge requests found
...@@ -30,9 +30,39 @@ class HomeRoot extends Component { ...@@ -30,9 +30,39 @@ class HomeRoot extends Component {
componentDidMount() { componentDidMount() {
this.loadEnvironments(); this.loadEnvironments();
this.setState({ dialogType: this.props.dialogType }); this.setState({ dialogType: this.props.dialogType });
document.querySelector("#background-video").playbackRate = 0.75; this.loadHomeVideo();
} }
loadHomeVideo = () => {
const videoEl = document.querySelector("#background-video");
function initVideo() {
videoEl.playbackRate = 0.75;
videoEl.play();
let inIframe = false;
try {
inIframe = !!window.frameElement || window.self !== window.top;
} catch (e) {
inIframe = false;
}
function toggleVideo() {
// Play the video if the window/tab is visible (or is within an `<iframe>`).
if (document.hasFocus() || inIframe) {
videoEl.play();
} else {
videoEl.pause();
}
}
document.addEventListener("visibilitychange", toggleVideo);
window.addEventListener("focus", toggleVideo);
window.addEventListener("blur", toggleVideo);
}
if (videoEl.readyState >= videoEl.HAVE_FUTURE_DATA) {
initVideo();
} else {
videoEl.addEventListener("canplay", initVideo);
}
};
showDialog = dialogType => { showDialog = dialogType => {
return e => { return e => {
e.preventDefault(); e.preventDefault();
...@@ -181,7 +211,7 @@ class HomeRoot extends Component { ...@@ -181,7 +211,7 @@ class HomeRoot extends Component {
</div> </div>
</div> </div>
</div> </div>
<video playsInline autoPlay muted loop className="background-video" id="background-video"> <video playsInline muted loop className="background-video" id="background-video">
<source src={homeVideoWebM} type="video/webm" /> <source src={homeVideoWebM} type="video/webm" />
<source src={homeVideoMp4} type="video/mp4" /> <source src={homeVideoMp4} type="video/mp4" />
</video> </video>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment