I am learning development and have an uncaught load error on a video I have as a background of a website.
The goal is to have it automatically choose between two different videos on webpage load, but I seem to be missing something in the code below, as I am getting error: : Uncaught (in promise) DOMException: The play() request was interrupted by a new load request.
Can anyone help me to understand the error at hand, and how I can fix it?
HTML:
<video class="my-video" id="myVideo" autoplay loop muted preload="none" playsinline>
<source src="videolink1.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
JAVASCRIPT:
const video = document.getElementById("myVideo");
const videoSources = [
'https://videolink1.mp4',
'https://videolink2.mp4'
];
function changeVideoSourceRandomly() {
const randomIndex = Math.floor(Math.random() * videoSources.length);
const randomSource = videoSources[randomIndex];
const source = video.querySelector('source');
source.src = randomSource;
video.load();
}
changeVideoSourceRandomly();
I havent been understanding the google error result, could use some help understanding how to adjust my code.
2
Answers
This fixed my problems!
you have to pause the video before changing to a new video.
change your code to this and it should work.