Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add loader image to HTML5 video?

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 ?

like image 366
Rajat Gupta Avatar asked Nov 22 '11 17:11

Rajat Gupta


People also ask

How do you put an image over a video in HTML?

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.

How do I show loader when image is loading?

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.

How do you add content to a video in HTML?

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.


2 Answers

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>
like image 68
a paid nerd Avatar answered Sep 20 '22 12:09

a paid nerd


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

like image 42
Jamie-505 Avatar answered Sep 20 '22 12:09

Jamie-505