Is there a javascript way to check if a browser natively supports MP3?

后端 未结 2 1300
攒了一身酷
攒了一身酷 2020-12-09 18:40

If the browser can play MP3\'s then I\'ll use the audio tag to play a file. If not, I\'ll have to load in a flash player.

2条回答
  •  被撕碎了的回忆
    2020-12-09 19:23

    var audio  = document.createElement("audio"),
    canPlayMP3 = (typeof audio.canPlayType === "function" &&
                  audio.canPlayType("audio/mpeg;codecs=mp3") !== "");
    

    Edit:

    If you don't want to use JavaScript (yes, this will work in browsers that support but not MP3), try this:

    
    

    If you want auto-play, include an autoplay attribute on the audio element.

提交回复
热议问题