Due to HTML5 browser formats tricks I have to put fallback audio formats also in audio format. I want to set the src of source in audio programmatically but it is not worki
After you set the src attribute on the source element, call load() and then play() on the audio element. (Or, just load() if you have the autoplay attribute set.)
Have you tried document.getElementById("oggSource").src = 'OggFormat.ogg'
etc.?
Using .detach().appendTo(parent)
seems to work: http://jsfiddle.net/pimvdb/b7Jgh/.
$("#oggSource").attr("src", "foo.ogg").detach().appendTo("#audioPlayer");
I guess the browser only starts loading (and playing with autoplay
) if a <source>
element is added, not when it is just modified. I'm not sure why though, but appending it after detaching works.
Edit: You can also directly do .appendTo
since an element is unique (i.e. it has to be detached anyway): http://jsfiddle.net/pimvdb/b7Jgh/6/.
function updateSource(source, src) {
source = $(source);
source.attr("src", src).appendTo(source.parent());
}