I need to have a 10 sec video loop on the hero section of this website but it wont play on initial page load but after I navigate to another route and come back, then the video starts playing. (with autoplay attribute).
I have tried the same video with a 10x smaller file size so it cannot be because of the file size I assume.
I also tried loading the video with XMLHttpRequest like this:
const video = document.createElement('video')
var req = new XMLHttpRequest();
req.open('GET', 'video.mp4', true);
req.responseType = 'blob';
req.onload = function() {
if (this.status === 200) {
var videoBlob = this.response;
var vid = URL.createObjectURL(videoBlob);
video.src = vid;
const parentEl = document.querySelector('.video');
parentEl.appendChild(video);
}
}
req.onerror = function() {
// Error
}
req.send();
And I also tried like this:
const video = document.createElement('video');
video.src = '/intro_online.mp4';
console.log(video)
video.addEventListener('canplaythrough', (e) => {
video.autoplay = true;
video.preload = "auto"
const parentEl = document.querySelector('.video');
parentEl.appendChild(video);
});
But none of these solves the problem. What could I try to get over this problem?