changing source on html5 video tag

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

    Instead of getting the same video player to load new files, why not erase the entire <video> element and recreate it. Most browsers will automatically load it if the src's are correct.

    Example (using Prototype):

    var vid = new Element('video', { 'autoplay': 'autoplay', 'controls': 'controls' });
    var src = new Element('source', { 'src': 'video.ogg', 'type': 'video/ogg' });
    
    vid.update(src);
    src.insert({ before: new Element('source', { 'src': 'video.mp4', 'type': 'video/mp4' }) });
    
    $('container_div').update(vid);
    
    0 讨论(0)
  • 2020-11-22 09:22

    I have been researching this for quite a while and I am trying to do the same thing, so hopefully this will help someone else. I have been using crossbrowsertesting.com and literally testing this in almost every browser known to man. The solution I've got currently works in Opera, Chrome, Firefox 3.5+, IE8+, iPhone 3GS, iPhone 4, iPhone 4s, iPhone 5, iPhone 5s, iPad 1+, Android 2.3+, Windows Phone 8.

    Dynamically Changing Sources

    Dynamically changing the video is very difficult, and if you want a Flash fallback you will have to remove the video from the DOM/page and re-add it so that Flash will update because Flash will not recognize dynamic updates to Flash vars. If you're going to use JavaScript to change it dynamically, I would completely remove all <source> elements and just use canPlayType to set the src in JavaScript and break or return after the first supported video type and don't forget to dynamically update the flash var mp4. Also, some browsers won't register that you changed the source unless you call video.load(). I believe the issue with .load() you were experiencing can be fixed by first calling video.pause(). Removing and adding video elements can slow down the browser because it continues buffering the removed video, but there's a workaround.

    Cross-browser Support

    As far as the actual cross-browser portion, I arrived at Video For Everybody as well. I already tried the MediaelementJS Wordpress plugin, which turned out to cause a lot more issues than it resolved. I suspect the issues were due to the Wordpress plug-in and not the actually library. I'm trying to find something that works without JavaScript, if possible. So far, what I've come up with is this plain HTML:

    <video width="300" height="150" controls="controls" poster="http://sandbox.thewikies.com/vfe-generator/images/big-buck-bunny_poster.jpg" class="responsive">
    <source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.ogv" type="video/ogg" />
    <source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4" type="video/mp4" />
    <source src="http://clips.vorwaerts-gmbh.de/big_buck_bunny.webm" type="video/webm" />
    <source src="http://alex-watson.net/wp-content/uploads/2014/07/big_buck_bunny.iphone.mp4" type="video/mp4" />
    <source src="http://alex-watson.net/wp-content/uploads/2014/07/big_buck_bunny.iphone3g.mp4" type="video/mp4" />
    <object type="application/x-shockwave-flash" data="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" width="561" height="297">
        <param name="movie" value="http://releases.flowplayer.org/swf/flowplayer-3.2.1.swf" />
        <param name="allowFullScreen" value="true" />
        <param name="wmode" value="transparent" />
        <param name="flashVars" value="config={'playlist':['http://sandbox.thewikies.com/vfe-generator/images/big-buck-bunny_poster.jpg',{'url':'http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4','autoPlay':false}]}" />
        <img alt="No Video" src="http://sandbox.thewikies.com/vfe-generator/images/big-buck-bunny_poster.jpg" width="561" height="297" title="No video playback capabilities, please download the video below" />
    </object>
    <strong>Download video:</strong>  <a href="video.mp4">MP4 format</a> | <a href="video.ogv">Ogg format</a> | <a href="video.webm">WebM format</a>
    </video>
    

    Important notes:

    • Ended up putting the ogg as the first <source> because Mac OS Firefox quits trying to play the video if it encounters an MP4 as the first <source>.
    • The correct MIME types are important .ogv files should be video/ogg, not video/ogv
    • If you have HD video, the best transcoder I've found for HD quality OGG files is Firefogg
    • The .iphone.mp4 file is for iPhone 4+ which will only play videos that are MPEG-4 with H.264 Baseline 3 Video and AAC audio. The best transcoder I found for that format is Handbrake, using the iPhone & iPod Touch preset will work on iPhone 4+, but to get iPhone 3GS to work you need to use the iPod preset which has much lower resolution which I added as video.iphone3g.mp4.
    • In the future we will be able to use a media attribute on the <source> elements to target mobile devices with media queries, but right now the older Apple and Android devices don't support it well enough.

    Edit:

    • I'm still using Video For Everybody but now I've transitioned to using FlowPlayer, to control the Flash fallback, which has an awesome JavaScript API that can be used to control it.
    0 讨论(0)
  • 2020-11-22 09:23

    Using JavaScript and jQuery:

    <script src="js/jquery.js"></script>
    ...
    <video id="vid" width="1280" height="720" src="v/myvideo01.mp4" controls autoplay></video>
    ...
    function chVid(vid) {
        $("#vid").attr("src",vid);
    }
    ...
    <div onclick="chVid('v/myvideo02.mp4')">See my video #2!</div>
    
    0 讨论(0)
  • 2020-11-22 09:25

    I solved this with this simple method

    function changeSource(url) {
       var video = document.getElementById('video');
       video.src = url;
       video.play();
    }
    
    0 讨论(0)
  • 2020-11-22 09:26

    Just put a div and update the content...

    <script>
    function setvideo(src) {
        document.getElementById('div_video').innerHTML = '<video autoplay controls id="video_ctrl" style="height: 100px; width: 100px;"><source src="'+src+'" type="video/mp4"></video>';
        document.getElementById('video_ctrl').play();
    }
    </script>
    <button onClick="setvideo('video1.mp4');">Video1</button>
    <div id="div_video"> </div>
    
    0 讨论(0)
  • 2020-11-22 09:27

    Modernizr worked like a charm for me.

    What I did is that I didn't use <source>. Somehow this screwed things up, since the video only worked the first time load() was called. Instead I used the source attribute inside the video tag -> <video src="blabla.webm" /> and used Modernizr to determine what format the browser supported.

    <script>
    var v = new Array();
    
    v[0] = [
            "videos/video1.webmvp8.webm",
            "videos/video1.theora.ogv",
            "videos/video1.mp4video.mp4"
            ];
    v[1] = [
            "videos/video2.webmvp8.webm",
            "videos/video2.theora.ogv",
            "videos/video2.mp4video.mp4"
            ];
    v[2] = [
            "videos/video3.webmvp8.webm",
            "videos/video3.theora.ogv",
            "videos/video3.mp4video.mp4"
            ];
    
    function changeVid(n){
        var video = document.getElementById('video');
    
        if(Modernizr.video && Modernizr.video.webm) {
            video.setAttribute("src", v[n][0]);
        } else if(Modernizr.video && Modernizr.video.ogg) {
            video.setAttribute("src", v[n][1]);
        } else if(Modernizr.video && Modernizr.video.h264) {
            video.setAttribute("src", v[n][2]);
        }
    
        video.load();
    }
    </script>
    

    Hopefully this will help you :)

    If you don't want to use Modernizr , you can always use CanPlayType().

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