How to play a notification sound on websites?

前端 未结 11 1512
生来不讨喜
生来不讨喜 2020-11-28 01:32

When a certain event occurs, I want my website to play a short notification sound to the user.

The sound should not auto-start (instantly) when the

11条回答
  •  一向
    一向 (楼主)
    2020-11-28 02:25

    2020 solution

    function playSound(url) {
      const audio = new Audio(url);
      audio.play();
    }
    
      
    

    Browser support

    Edge 12+, Firefox 20+, Internet Explorer 9+, Opera 15+, Safari 4+, Chrome

    Codecs Support

    Just use MP3


    Old solution

    (for legacy browsers)

    function playSound(filename){
      var mp3Source = '';
      var oggSource = '';
      var embedSource = '';
      document.getElementById("sound").innerHTML='';
    }
    
      
    

    Browser support

    • (Fallback)

    Codes used

    • MP3 for Chrome, Safari and Internet Explorer.
    • OGG for Firefox and Opera.

提交回复
热议问题