I'm using the following code to play a soundfile:
var audio = new Audio();
audio.src = 'somePath/filename.ogg';
audio.volume = 10;
audio.autoPlay = false;
audio.preLoad = true;
// ...
audio.play();
And it works well. However, some browsers may not support ogg format, so I want to add mp3 format as alternative, too. How can I do this with javascript though?
For reference, this is how it would look like in pure HTML5 when you offer multiple formats:
<audio volume="10" preload="auto">
<source src="filename.ogg" type="audio/ogg">
<source src="filename.mp3" type="audio/mpeg">
</audio>
So, basically instead of setting an audio.src I will need to add <source> elements to the Audio object. How do I do that though? Is there anything like a new Source() thing in javascript that I need to use here that I can somehow add to audio?
Bonus question: If the browser supports none of the offered source formats, can I somehow execute some custom code, like printing a message to the user, saying that their browser sucks? :)