How to add loader image to HTML5 video?
Asked Answered
Q

6

23

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 ?

Quartziferous answered 22/11, 2011 at 17:6 Comment(0)
S
32

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>
Santosantonica answered 22/11, 2011 at 19:53 Comment(0)
S
10

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

Soleure answered 24/10, 2014 at 10:20 Comment(0)
I
6

It took me a way too long to actually figure out how to do this, but I'm going to share it here because I FINALLY found a way! Which is ridiculous when you think about it, because loading is something that all videos have to do. You'd think they would have taken this into account when creating the html5 video standard.

My original theory that I thought should have worked (but wouldn't) was this

  1. Add loading bg image to video when loading via js and css
  2. Remove when ready to play

Simple, right? The problem was that I couldn't get the background image to show when the source elements were set, or the video.src attribute was set. The final stroke of genius/luck was to find out (through experimentation) that the background-image will not disappear if the poster is set to something. I'm using a fake poster image, but I imagine it would work as well with a transparent 1x1 image (but why worry about having another image). So this makes this probably a kind of hack, but it works and I don't have to add extra markup to my code, which means it will work across all my projects using html5 video.

HTML

<video controls="" poster="data:image/gif,AAAA">
    <source src="yourvid.mp4"
</video>

CSS (loading class applied to video with JS)

video.loading {
  background: black url(/images/loader.gif) center center no-repeat;
}

JS

  $('#video_id').on('loadstart', function (event) {
    $(this).addClass('loading')
  });
  $('#video_id').on('canplay', function (event) {
    $(this).removeClass('loading')
  });

This works perfectly for me but only tested in chrome and safari on mac. Let me know if anyone finds bugs and or improvements!

Instigation answered 28/6, 2014 at 19:38 Comment(1)
Thanks for the code , BTW no need for JS code, you can warp video block with div container and change it background .Raasch
A
3

You could attach a listener to the video's loadstart event and use it to overlay an animated GIF on top of the video element, then hide the loading overlay once the loadeddata event fires. See The W3C's media events draft for a list of events you can hook into.

They also have a demo page for media events.

Assortment answered 22/11, 2011 at 17:37 Comment(0)
F
1

This is quite complicated, you must listen to various video events to show/hide & update width of the loader image correctly. Those events include (but may not limited to): loadstart, progress, loadeddata, waiting, seeking, seeked. You can use a certain open source player (e.g. jPlayer) or download its source to examine further.

Fabulist answered 22/11, 2011 at 18:7 Comment(0)
M
0
<video
  id="mainVideo"
  width="100%"
  style="max-width: 100%; max-height: 100%;"
  preload="auto" 
  autoplay="autoplay"
  (loadeddata)= "checkVideoLoaded()"
>
    <source [src]="assets?.video?.urlMP4" type="{{videoType}}">
</video>

on isVideoLoaded flag show and hide loader

this.isVideoLoaded = false;
    
checkVideoLoaded(){
  this.isVideoLoaded = true;
}
Misprize answered 31/7, 2018 at 5:16 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.