JSFiddle - React, Tailwind, and code Playground
by phated
JavaScript
var audio = new Audio();
var sounds = [
{ type: 'audio/mpeg', filename: 'epic-theme.mp3' },
// adding ; codecs="vorbis" to webm would make it "probably"
{ type: 'audio/webm', filename: 'epic-theme.webm' }
];
var sources = sounds.reduce(function(result, sound){
var canPlay = audio.canPlayType(sound.type);
if(canPlay === 'probably') result.unshift(sound.filename);
if(canPlay === 'maybe') result.push(sound.filename);
return result;
}, []);
audio.addEventListener('error', setSrc);
function setSrc(){
sources.length && (audio.src = sources.shift());
}
setSrc();