HTML5 Audio in JavaScript: What am I doing wrong?
I'm trying to create an HTML 5 audio tag in Javascript. I'm having trouble getting it to work and to preload:
var audioElement = document.createElement('audio');
var source1 = document.createElement('source');
source1.type= 'audio/ogg';
source1.src= 'assets/audio/ost.ogg';
source1.setAttribute("preload","auto");
audioElement.appendChild(source1);
var source2 = document.createElement('source');
source2.type= 'audio/mpeg';
source2.src= 'assets/audio/ost开发者_C百科.mp3';
source2.setAttribute("preload","auto");
audioElement.appendChild(source2);
audioElement.preload = auto;
audioElement.load();
Any ideas?
Thank you.
EDIT:
Here's what I ended up doing for anyone wondering. Works in FF3.6, ff4, safari 5, ie9, chrome 11, opera 11.11 (pc)
var audioElement = document.createElement('audio');
audioElement.setAttribute("preload", "auto");
audioElement.autobuffer = true;
var source1 = document.createElement('source');
source1.type= 'audio/ogg';
source1.src= 'assets/audio/ost.ogg';
audioElement.appendChild(source1);
var source2 = document.createElement('source');
source2.type= 'audio/mpeg';
source2.src= 'assets/audio/ost.mp3';
audioElement.appendChild(source2);
And then:
audioElement.load();
Thanks for your help, Kevin.
<source>
elements can't have the attribute preload
and you forgot to put quotes around the preload property on your audio object.
So, remove the setAttribute('preload', 'auto')
from your sourceX
objects and put quotes around the preload
attribute, like so:
audioElement.preload = 'auto';
- MDC Audio element
- MDC Source element
精彩评论