Making HTML5 Canvas Canvas

Sunday, September 08, 2013

Appendix 1: Audio Compatibility

About the sounds, we must highlight an important problem, because now days, Internet Explorer and Safari browsers only admit the mp3 and mp4 (m4a/aac) formats, while Firefox and Opera only admit the ogg (oga) format. Chrome admits both formats, so there is no big problem with it.

Therefore, when adding sounds to our game, is possible that in some browsers we can't listen to them (Depending on the selected format).

There are advanced solutions that allow you to find out the user's browser, and select a different file depending on this. Here I give you the solution I use to know if I should use an ogg file or not, in case you want to use it too:
function canPlayOgg(){
    var aud=new Audio();
    if(aud.canPlayType('audio/ogg').replace(/no/,''))
        return true;
    else
        return false;
}
This function would be called this way:
    if(canPlayOgg())
        aEat.src="assets/chomp.oga";
    else
        aEat.src="assets/chomp.m4a";
You can find more alternatives trough the web, so you can use the one easier for you.

No comments:

Post a Comment