How to play a notification sound on websites?

前端 未结 11 1507
生来不讨喜
生来不讨喜 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:14

    if you want calling event on the code The best way to do that is to create trigger because the browser will not respond if the user is not on the page

    <button type="button" style="display:none" id="playSoundBtn" onclick="playSound();"></button>
    

    now you can trigger your button when you want to play sound

    $('#playSoundBtn').trigger('click');
    
    0 讨论(0)
  • 2020-11-28 02:14

    We can just use Audio and an object together like:

    var audio = {};
    audio['ubuntu'] = new Audio();
    audio['ubuntu'].src="start.ogg";
    audio['ubuntu'].play();
    

    and even adding addEventListener for play and ended

    0 讨论(0)
  • 2020-11-28 02:25

    2020 solution

    function playSound(url) {
      const audio = new Audio(url);
      audio.play();
    }
    
    <button onclick="playSound('https://your-file.mp3');">Play</button>  
    

    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 = '<source src="' + filename + '.mp3" type="audio/mpeg">';
      var oggSource = '<source src="' + filename + '.ogg" type="audio/ogg">';
      var embedSource = '<embed hidden="true" autostart="true" loop="false" src="' + filename +'.mp3">';
      document.getElementById("sound").innerHTML='<audio autoplay="autoplay">' + mp3Source + oggSource + embedSource + '</audio>';
    }
    
    <button onclick="playSound('bing');">Play</button>  
    <div id="sound"></div>
    

    Browser support

    • <audio> (Modern browsers)
    • <embed> (Fallback)

    Codes used

    • MP3 for Chrome, Safari and Internet Explorer.
    • OGG for Firefox and Opera.
    0 讨论(0)
  • 2020-11-28 02:25

    if you want to automate the process via JS:

    Include somewhere in the html:

    <button onclick="playSound();" id="soundBtn">Play</button>  
    

    and hide it via js :

    <script type="text/javascript">
    
    document.getElementById('soundBtn').style.visibility='hidden';
    
    function performSound(){
    var soundButton = document.getElementById("soundBtn");
    soundButton.click();
    }
    
    function playSound() {
          const audio = new Audio("alarm.mp3");
          audio.play();
        }
    
    </script>
    

    if you want to play the sound just call performSound() somewhere!

    0 讨论(0)
  • 2020-11-28 02:26

    I wrote a clean functional method of playing sounds:

    sounds = {
        test : new Audio('/assets/sounds/test.mp3')
    };
    
    sound_volume = 0.1;
    
    function playSound(sound) {
        sounds[sound].volume = sound_volume;
        sounds[sound].play();
    }
    function stopSound(sound) {
        sounds[sound].pause();
    }
    function setVolume(sound, volume) {
        sounds[sound].volume = volume;
        sound_volume = volume;
    }
    
    0 讨论(0)
提交回复
热议问题