javascript Audio object vs. HTML5 Audio tag

后端 未结 5 1909
刺人心
刺人心 2020-12-07 20:25

In a project recently when I loaded a sound with

var myAudio = new Audio(\"myAudio.mp3\");
myAudio.play();

It played fine unless a dialogu

5条回答
  •  悲哀的现实
    2020-12-07 20:49

    I came up with the function below from several answers across the web.

    function playAudio(url){
      var audio = document.createElement('audio');
      audio.src = url;
      audio.style.display = "none"; //added to fix ios issue
      audio.autoplay = false; //avoid the user has not interacted with your page issue
      audio.onended = function(){
        audio.remove(); //remove after playing to clean the Dom
      };
      document.body.appendChild(audio);
    }
    

提交回复
热议问题