changing source on html5 video tag

前端 未结 16 2064
既然无缘
既然无缘 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: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:

    
    

    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);
    

提交回复
热议问题