changing source on html5 video tag

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

    This is my solution:

    <video id="playVideo" width="680" height="400" controls="controls">
        <source id="sourceVideo" src="{{video.videoHigh}}" type="video/mp4">
    </video>
        <br />
    <button class="btn btn-warning" id="{{video.videoHigh}}" onclick="changeSource(this)">HD</button>
    <button class="btn btn-warning" id="{{video.videoLow}}" onclick="changeSource(this)">Regular</button>
    
    <script type="text/javascript">
        var getVideo = document.getElementById("playVideo");
        var getSource = document.getElementById("sourceVideo");
        function changeSource(vid) {
            var geturl = vid.id;
            getSource .setAttribute("src", geturl);
            getVideo .load()
            getVideo .play();
            getVideo .volume = 0.5;
        }
    </script>
    
    0 讨论(0)
  • 2020-11-22 09:03

    Using the <source /> tags proved difficult for me in Chrome 14.0.835.202 specifically, although it worked fine for me in FireFox. (This could be my lack of knowledge, but I thought an alternate solution might be useful anyway.) So, I ended up just using a <video /> tag and setting the src attribute right on the video tag itself. The canPlayVideo('<mime type>') function was used to determine whether or not the specific browser could play the input video. The following works in FireFox and Chrome.

    Incidently, both FireFox and Chrome are playing the "ogg" format, although Chrome recommends "webm". I put the check for browser support of "ogg" first only because other posts have mentioned that FireFox prefers the ogg source first (i.e. <source src="..." type="video/ogg"/> ). But, I haven't tested (and highly doubt) whether or not it the order in the code makes any difference at all when setting the "src" on the video tag.

    HTML

    <body onload="setupVideo();">
        <video id="media" controls="true" preload="auto" src="">
        </video>
    </body>
    

    JavaScript

    function setupVideo() {
           // You will probably get your video name differently
           var videoName = "http://video-js.zencoder.com/oceans-clip.mp4";
    
           // Get all of the uri's we support
           var indexOfExtension = videoName.lastIndexOf(".");
           //window.alert("found index of extension " + indexOfExtension);
           var extension = videoName.substr(indexOfExtension, videoName.length - indexOfExtension);
           //window.alert("extension is " + extension);
           var ogguri = encodeURI(videoName.replace(extension, ".ogv"));
           var webmuri = encodeURI(videoName.replace(extension, ".webm"));
           var mp4uri = encodeURI(videoName.replace(extension, ".mp4"));
           //window.alert(" URI is " + webmuri);
    
    
           // Get the video element
           var v = document.getElementById("media");
           window.alert(" media is " + v);
    
           // Test for support
           if (v.canPlayType("video/ogg")) {
                v.setAttribute("src", ogguri);
               //window.alert("can play ogg");
           }
           else if (v.canPlayType("video/webm")) {
               v.setAttribute("src", webmuri);
               //window.alert("can play webm");
           }
           else if (v.canPlayType("video/mp4")) {
               v.setAttribute("src", mp4uri);
               //window.alert("can play mp4");
           }
           else {
               window.alert("Can't play anything");
           }
    
          v.load();
          v.play();
      }
    
    0 讨论(0)
  • 2020-11-22 09:04

    According to the spec

    Dynamically modifying a source element and its attribute when the element is already inserted in a video or audio element will have no effect. To change what is playing, just use the src attribute on the media element directly, possibly making use of the canPlayType() method to pick from amongst available resources. Generally, manipulating source elements manually after the document has been parsed is an unncessarily complicated approach.

    So what you are trying to do is apparently not supposed to work.

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

    I have a similar web app and am not facing that sort of problem at all. What i do is something like this:

    var sources = new Array();
    
    sources[0] = /path/to/file.mp4
    sources[1] = /path/to/another/file.ogg
    etc..
    

    then when i want to change the sources i have a function that does something like this:

    this.loadTrack = function(track){
    var mediaSource = document.getElementsByTagName('source')[0];
    mediaSource.src = sources[track];
    
        var player = document.getElementsByTagName('video')[0];
        player.load();
    
    }
    

    I do this so that the user can make their way through a playlist, but you could check for userAgent and then load the appropriate file that way. I tried using multiple source tags like everyone on the internet suggested, but i found it much cleaner, and much more reliable to manipulate the src attribute of a single source tag. The code above was written from memory, so i may have glossed over some of hte details, but the general idea is to dynamically change the src attribute of the source tag using javascript, when appropriate.

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

    I come with this to change video source dynamically. "canplay" event sometime doesn't fire in Firefox so i have added "loadedmetadata". Also i pause previous video if there is one...

    var loadVideo = function(movieUrl) {
        console.log('loadVideo()');
        $videoLoading.show();
        var isReady = function (event) {
                console.log('video.isReady(event)', event.type);
                video.removeEventListener('canplay', isReady);
                video.removeEventListener('loadedmetadata', isReady);
                $videoLoading.hide();
                video.currentTime = 0;
                video.play();
            },
            whenPaused = function() {
                console.log('video.whenPaused()');
                video.removeEventListener('pause', whenPaused);
                video.addEventListener('canplay', isReady, false);
                video.addEventListener('loadedmetadata', isReady, false); // Sometimes Firefox don't trigger "canplay" event...
                video.src = movieUrl; // Change actual source
            };
    
        if (video.src && !video.paused) {
            video.addEventListener('pause', whenPaused, false);
            video.pause();
        }
        else whenPaused();
    };
    
    0 讨论(0)
  • 2020-11-22 09:18

    Your original plan sounds fine to me. You'll probably find more browser quirks dealing with dynamically managing the <source> elements, as indicated here by the W3 spec note:

    Dynamically modifying a source element and its attribute when the element is already inserted in a video or audio element will have no effect. To change what is playing, just use the src attribute on the media element directly, possibly making use of the canPlayType() method to pick from amongst available resources. Generally, manipulating source elements manually after the document has been parsed is an unncessarily[sic] complicated approach.

    http://dev.w3.org/html5/spec/Overview.html#the-source-element

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