Along with the poster image I want a loader image(an animated gif) to be displayed while the video is being downloaded. How is it possible ?
There is a simple video attribute that allows the use of pre-loaded images. The "poster" attribute defines a poster image that is in place of the video. Show activity on this post. The poster attribute specifies an image to be shown while the video is downloading, or until the user hits the play button.
Basically we set the initial image source to the loader image. Once the image loads(load event triggered), we listen to it and set the image source to the actual image. 2. Now use the just need to use the created attributes on your images.
Use the <video> tag for inserting videos in HTML That is to say that the video is identified by adding a video URL to a source attribute.
A cheap way could be to use an animated GIF in the poster
attribute which will be replaced when the video begins to play. Example:
<video poster="loading.gif" preload="auto" controls autoplay>
<source type="video/mp4" src="video.mp4"/>
</video>
Here is my solution for this problem since pixelearth's answer doesn't seem to work on firefox (probably a problem with the fake poster)
HTML
<video id="video1" height="236" preload="auto" controls>
<source src="yourVideo.mp4">
Your browser does not support HTML5 video.
</video>
JS
$('#video1').on('loadstart', function (event) {
$(this).addClass('background');
$(this).attr("poster", "/your/loading.gif");
});
$('#video1').on('canplay', function (event) {
$(this).removeClass('background');
$(this).removeAttr("poster");
});
CSS
video.background {
background: black
}
With this answer you don't have to mess around with fake poster or abuse attributes for purposes that they were not made for. Hope this helps.
P.S. You could of course add some more events to add the poster attribute e.g. if it can't play further in the middle of the video and needs more buffering
You can find a jsfiddle which shows my code here
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With