I've been updating my site and making it HTML5 compliant, plus upgrading the jQ/js, giving it a much smoother experience. One of the additions I've done is adding small subtle audio feedback cues. Like a computer that beeps when you do something wrong, so do my network tests beep when you submit invalid data. This is what I'm using for the HTML:
<audio id="audio_error" preload="auto">
<source src="http://<? echo $_SERVER['HTTP_HOST']; ?>/audio/error.ogg" type="audio/ogg">
<source src="http://<? echo $_SERVER['HTTP_HOST']; ?>/audio/error.mp3" type="audio/mp3">
</audio>
Then I play the audio with this:
document.getElementById('audio_error').play();
It works very well, with the obvious exception: IE.
Specifically, older versions of IE don't know how to support this, and so when you get to the play() command, all js stops. It's very annoying. I've eeked around this so far by always making the audio play command the last thing that is run. But as my scripts get more and more complicated, it's time that I need a real solution.
So I made a wrapper function, with the idea that I'd somehow check to see if .play() was supported, before trying to play the audio. If it's not supported, no worries, just do nothing. I already do some class-changing to give visual feedback. The audio is just a bonus.
So I tried two different things with my wrapper function, and neither worked. Neither would work reliably that is. They'd work, then not work, without me having made any more changes in code. And this odd behavior was not just in IE, these wrapper functions tanked even in modern browsers.
function play_a_sound($sound)
{
if (typeof play == "function")
{ document.getElementById($sound).play(); }
else
{ alert('audio fail'); }
}
and
function play_a_sound($sound)
{
if (window.HTMLAudioElement)
{ document.getElementById($sound).play(); }
else
{ alert('audio fail'); }
}
The $sound I passed to the function was simply a string of the ID of the sound I wanted to play.
The "alert()"s above are just for debugging, both functions would simply do nothing audio was not supported. But like I said, these two wrapper functions would not work reliably, not just in IE, but in modern browsers too. Sometimes they'd work, sometimes they wouldn't. Sometimes I'd get audio once, and not again.
Clearly I'm barking up the wrong tree here. What am I doing wrong?