I have a 45 second video that I was able to compress to 4 Mo. I want the video to play smoothly and with autoplay, but it doesn't need to launch quickly as long as the poster image is present. So once it is fully loaded it should automatically play. I found this bit of javascript code but I am getting errors and I don't understand why.
<div class="video-container">
<video id="myvideo" controls autoplay muted poster="image.jpg" playsinline style="width: 100% !important;">
</video> </div>
<script type="text/javascript">
var req = new XMLHttpRequest();
req.open('GET', 'example.com/wp-content/uploads/2019/07/video.mp4', true);
req.responseType = 'blob';
req.onload = function() {
// Onload is triggered even on 404
// so we need to check the status code
if (this.status === 200) {
var videoBlob = this.response;
var vid = URL.createObjectURL(videoBlob); // IE10+
// Video is now downloaded
// and we can set it as source on the video element
video.src = vid;
}
}
req.onerror = function() {
// Error
}
req.send();
document.getElementById("myvideo").src = vid;
</script>
I am getting the error that the video is not defined on the line video.src = vid; and subsequently on the line document.getElementById("myvideo").src = vid;
In the Console the video has correctly been charged to the page, between 1 to 3 seconds of upload.