changing source on html5 video tag

前端 未结 16 1843
既然无缘
既然无缘 2020-11-22 08:31

i\'m trying to build a video player, that works everywhere. so far i\'d be going with:

相关标签:
16条回答
  • 2020-11-22 09:27

    Yaur: Although what you have copied and pasted is good advice, this does not mean that it is impossible to change the source element of an HTML5 video element elegantly, even in IE9 (or IE8 for that matter).(This solution does NOT involve replacing the entire video element, as it is bad coding practice).

    A complete solution to changing/switching videos in HTML5 video tags via javascript can be found here and is tested in all HTML5 browser (Firefox, Chrome, Safari, IE9, etc).

    If this helps, or if you're having trouble, please let me know.

    0 讨论(0)
  • 2020-11-22 09:29

    I hated all these answers because they were too short or relied on other frameworks.

    Here is "one" vanilla JS way of doing this, working in Chrome, please test in other browsers:

    http://jsfiddle.net/mattdlockyer/5eCEu/2/

    HTML:

    <video id="video" width="320" height="240"></video>
    

    JS:

    var video = document.getElementById('video');
    var source = document.createElement('source');
    
    source.setAttribute('src', 'http://www.tools4movies.com/trailers/1012/Kill%20Bill%20Vol.3.mp4');
    
    video.appendChild(source);
    video.play();
    
    setTimeout(function() {  
        video.pause();
    
        source.setAttribute('src', 'http://www.tools4movies.com/trailers/1012/Despicable%20Me%202.mp4'); 
    
        video.load();
        video.play();
    }, 3000);
    
    0 讨论(0)
  • 2020-11-22 09:29

    Try moving the OGG source to the top. I've noticed Firefox sometimes gets confused and stops the player when the one it wants to play, OGG, isn't first.

    Worth a try.

    0 讨论(0)
  • 2020-11-22 09:29

    Another way you can do in Jquery.

    HTML

    <video id="videoclip" controls="controls" poster="" title="Video title">
        <source id="mp4video" src="video/bigbunny.mp4" type="video/mp4"  />
    </video>
    
    <div class="list-item">
         <ul>
             <li class="item" data-video = "video/bigbunny.mp4"><a href="javascript:void(0)">Big Bunny.</a></li>
         </ul>
    </div>
    

    Jquery

    $(".list-item").find(".item").on("click", function() {
            let videoData = $(this).data("video");
            let videoSource = $("#videoclip").find("#mp4video");
            videoSource.attr("src", videoData);
            let autoplayVideo = $("#videoclip").get(0);
            autoplayVideo.load();
            autoplayVideo.play();
        });
    
    0 讨论(0)
提交回复
热议问题