How to make a loading image when loading HTML5 video?

后端 未结 6 1902
谎友^
谎友^ 2020-12-14 18:16

As it takes time for the video player to load the mp4 video, does HTML5 support playing a \"loading\" logo when loading the video ?

相关标签:
6条回答
  • 2020-12-14 18:25

    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).attr('poster', '');
      });
    

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

    0 讨论(0)
  • 2020-12-14 18:29

    Use the tag id poster

        <video controls="controls" poster="/IMG_LOCATION/IMAGENAME">
    

    More info can be found http://www.w3schools.com/html5/att_video_poster.asp

    0 讨论(0)
  • 2020-12-14 18:36

    Also a simple solution to add a preloader image while loading the video: HTML:

    <video class="bg_vid" autoplay loop poster="images/FFFFFF-0.png">
    

    the poster is a transparent image 1px.

    CSS:

    video {
        background-image: url(images/preload_30x30.gif);
        background-repeat: no-repeat;
        background-size: 30px 30px;
        background-position: center;
    }
    
    0 讨论(0)
  • 2020-12-14 18:38

    You could probably do it with JavaScript by creating an image overlay with an animated "loading" gif which you only display when the video is loading and is not ready to play.

    You'd have to write JavaScript to link up with the Media API for detecting when the video is ready to play and so forth though (so you could hide the image again), but it should be possible.

    0 讨论(0)
  • 2020-12-14 18:45

    Personally I think the most elegant way to do this is by using some code like this,

    <video src="myVideo.fileExtension" onplaying="hideControls(this)" onwaiting="showControls(this)" preload="auto" poster="myAnimatedWebpOrGifThatSaysVideoIsNotYetReady.fileExtension">No video support?</video>
    
    <script type="text/javascript">
    //We hide the video control buttons and the playhead when the video is playing (and enjoyed by the viewer)
    function hideControls(event){ event.controls=false; }
    //If the video has to pause and wait for data from the server we let controls be seen if the user hovers or taps on the video. As a bonus this also makes the built-in loading animation of the browser appear e.g. the rotating circular shape and we give it a little delay (like 1 sec) because we don't want the controls to blink and the delayed show-up actually looks nicer.
    function showControls(event){ setTimeout(function(){ event.controls=true; },1000); }
    </script>
    

    To make it even better you could use ontimeupdate instead of onplaying which would fire continuously. As for the delay time actually not 1 but 4 seconds -to me- is the best.

    0 讨论(0)
  • 2020-12-14 18:47

    My solution was to add the following inside of the window.fbAsyncInit = function():

    var finished_rendering = function() {
        var el = document.querySelector('div#loading_msg');
        el.style.display="none";
    }
    
    FB.Event.subscribe('xfbml.render', finished_rendering);
    

    Found: https://developers.facebook.com/docs/reference/javascript/FB.Event.subscribe/v2.12

    0 讨论(0)
提交回复
热议问题