i'm trying to build a video player, that works everywhere. so far i'd be going with:
<video>
<source src="video.mp4"></source>
<source src="video.ogv"></source>
<object data="flowplayer.swf" type="application/x-shockwave-flash">
<param name="movie" value="flowplayer.swf" />
<param name="flashvars" value='config={"clip":"video.mp4"}' />
</object>
</video>
(as seen on several sites, for example video for everybody) so far, so good.
but now i also want some kind of playlist/menu along with the video player, from which i can select other videos. those should be opened within my player right away. so i will have to "dynamically change the source of the video" (as seen on dev.opera.com/articles/everything-you-need-to-know-html5-video-audio/ - section "Let's look at another movie") with javascript. let's forget about the flashplayer (and thus IE) part for the time being, i will try to deal with that later.
so my JS to change the <source>
tags should be something like:
<script>
function loadAnotherVideo() {
var video = document.getElementsByTagName('video')[0];
var sources = video.getElementsByTagName('source');
sources[0].src = 'video2.mp4';
sources[1].src = 'video2.ogv';
video.load();
}
</script>
problem is, this doesnt work in all browsers. namely, firefox =O there is a nice page, where you can observe the problem i'm having: http://www.w3.org/2010/05/video/mediaevents.html
as soon as i trigger the load() method (in firefox, mind you), the video player dies.
now i have found out that when i don't use multiple <source>
tags, but instead just one src attribute within the <video>
tag, the whole thing DOES work in firefox.
so my plan is to just use that src attribute and determine the appropriate file using the canPlayType() function.
am i doing it wrong somehow or complicating things??
I solved this with this simple method
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 usecanPlayType
to set thesrc
in JavaScript andbreak
orreturn
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 callvideo.load()
. I believe the issue with.load()
you were experiencing can be fixed by first callingvideo.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:
<source>
because Mac OS Firefox quits trying to play the video if it encounters an MP4 as the first<source>
.video/ogg
, notvideo/ogv
.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 asvideo.iphone3g.mp4
.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:
Just put a div and update the content...
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.
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:
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:http://dev.w3.org/html5/spec/Overview.html#the-source-element