changing source on html5 video tag

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

    
    

    Important notes:

    • Ended up putting the ogg as the first because Mac OS Firefox quits trying to play the video if it encounters an MP4 as the first .
    • 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 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.

提交回复
热议问题