简体   繁体   中英

Cannot play sound in iOS Safari

I cannot play sound in iOS Safari. I've looked into similar issues on StackOverflow, most suggested that it could only work with user interactions(onclick, ontouchstart, etc.). But my code doesn't work even if the playing is initiated by onclick . Here is my code.

<html>
<head></head>
<body>
  <button onclick="playSound()">Play Sound</button>
  <script>
    function playSound() {
      var audio = new Audio("source.wav");
      audio.play();
    }
  </script>
</body>
</html>

The above code works in desktop browsers but failed in iOS Safari. Why? I'm using iOS 12.0.1

As @obscure suggested in the comment, iOS Safari doesn't work with wav files by default. I converted all the wav files to mp3 and it works now.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM