Read Dynamically changing source element
You can do something like
var video = document.getElementById('video');
var source = document.createElement('source');
source.setAttribute('src', 'something.mkv');
video.appendChild(source);
video.play();
To update the video source
video.pause();
source.setAttribute('src', 'something-else.mkv');
video.load();
video.play();
Credit and other approaches
Edit:
Here is a complete example
HTML
<video id="video" controls="true">
</video>
JS
// Initial Load
var vid = document.getElementById('video');
var source = document.createElement('source');
var initialVideoSourceUrl = 'https://gitlab.la-bw.de/pub/ingestlist/raw/f49935f4e2201c78c7a3bcf4e86ce9eae6a21b18/test/test-files/video/SampleVideo_1280x720_1mb.mkv' ;
source.setAttribute('src', initialVideoSourceUrl);
vid.appendChild(source);
vid.play();
vid.addEventListener('pause', (e) => {
if (e.target.currentSrc === initialVideoSourceUrl && e.currentTarget.currentTime > 2 && e.currentTarget.currentTime < 4) {
vid.pause();
source.setAttribute('src', 'https://gitlab.freedesktop.org/thiblahute/gst-plugins-base/raw/85a7b8f5622536de9b888f58c7c6ca1b15834b78/tests/files/test.mkv?inline=false');
vid.load();
vid.play();
}
});
JSFiddle