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??
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):
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.Hopefully this will help you :)
If you don't want to use Modernizr , you can always use CanPlayType().
This is my solution:
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. ThecanPlayVideo('<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
JavaScript
I have a similar web app and am not facing that sort of problem at all. What i do is something like this:
then when i want to change the sources i have a function that does something like this:
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.
According to the spec
So what you are trying to do is apparently not supposed to work.