javascripthtmlmp3sniffer

Detecting MP3 support in a page for old browser


I'm writing an mp3 playlist player, and I'm almost done... but, I would like to make the page compatible with older browser (people with older browsers will see a simple list of mp3 links: they can download them, it's not a problem). Now, I'm afraid I have some problem with javascript syntax, rather than with some specific command. So, this is my code:

ilikemp3 = 0;

if (document.createElement('audio').canPlayType('audio/mpeg;codecs="mp3"'))
{ilikemp3 = 1};

if (ilikemp3)
{audio_playlist_html5()}
else
{simple_audio_list()};

It's working fine on modern browsers... but, if I run it in older browsers, I get an error message:

Error: document.createElement("audio").canPlayType is not a function

So, I understand that's not the proper way to query a browser. Can somebody help me? Thanks a lot!


Solution

  • You could check for the existence of canPlayType in document.createElement('audio') and decide based on that.

    if('canPlayType' in document.createElement('audio')) {
      //new player
    }
    else {
      //old player
    }