<audio controls>
<source src="foo.ogg" type="audio/ogg"/>
<source src="foo.mp3" type="audio/mp3"/>
Sorry, you're browser doesn't support ogg or mp3 codecs
(or possibly audio tags).
</audio>
function canPlayOgg () {
var a = document.createElement('audio');
return !!(a.canPlayType &&
a.canPlayType('audio/ogg; codecs="vorbis"')
.replace(/no/, ''));
};
canPlayOgg(); // true or false
More feature detection
<audio controls>
<source src="../media/lateralus_clip.ogg" type="audio/ogg"/>
<source src="../media/lateralus_clip.mp3" type="audio/mp3"/>
</audio>
<audio id="noControls">
<source src="../media/lateralus_clip.ogg" type="audio/ogg"/>
<source src="../media/lateralus_clip.mp3" type="audio/mp3"/>
</audio>
<button id="play">Play/Pause</button>
<button id="volUp">Volume+</button>
<button id="volDown">Volume->/button>
<input id="seek" type="range" value="" max=""/>
<script>
var audio = document.getElementById("noControls");
var play = document.getElementById("play");
var volUp = document.getElementById("volUp");
var volDown = document.getElementById("volDown");
var seek = document.getElementById("seek");
var playing = false;
play.addEventListener("click", function (e) {
playing = !playing;
if (playing) {
audio.play();
} else {
audio.pause();
}
});
function changeVol (up) {
if (up) {
audio.volume += 0.1;
} else {
audio.volume -= 0.1;
}
};
volUp.addEventListener("click", function () {
changeVol(true);
});
volDown.addEventListener("click", function () {
changeVol(false);
});
audio.addEventListener("timeupdate", function (e) {
seek.value = audio.currentTime;
});
seek.min = seek.value = audio.seekable.start(0);
seek.max = audio.seekable.end(0);
seek.addEventListener("change", function (e) {
audio.currentTime = seek.value;
});
</script>
<audio id="audioA" controls>
<source src="../media/lateralus_clip.ogg" type="audio/ogg"/>
<source src="../media/lateralus_clip.mp3" type="audio/mp3"/>
</audio>
<br/>
<audio id="audioB" controls>
<source src="../media/lateralus_clip.ogg" type="audio/ogg"/>
<source src="../media/lateralus_clip.mp3" type="audio/mp3"/>
</audio>
<script>
var audioA = document.getElementById("audioA");
var audioB = document.getElementById("audioB");
audioA.addEventListener("ended", function () {
audioB.currentTime = 0;
audioB.play();
});
audioB.addEventListener("ended", function () {
audioA.currentTime = 0;
audioA.play();
});
</script>
More events at Are We Playing Yet?
<video controls>
<source src="../media/bunny.webm" type="video/webm"/>
<source src="../media/bunny.mp4" type="video/mp4"/>
</video>