How to play a notification sound on websites?

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

    Use the audio.js which is a polyfill for the <audio> tag with fallback to flash.

    In general, look at https://github.com/Modernizr/Modernizr/wiki/HTML5-Cross-Browser-Polyfills for polyfills to the HTML 5 APIs.. (it includes more <audio> polyfills)

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

    How about the yahoo's media player Just embed yahoo's library

    <script type="text/javascript" src="http://mediaplayer.yahoo.com/js"></script> 
    

    And use it like

    <a id="beep" href="song.mp3">Play Song</a>
    

    To autostart

    $(function() { $("#beep").click(); });
    
    0 讨论(0)
  • 2020-11-28 02:08
    var audio = new Audio('audio_file.mp3');
    
    function post()
    {
      var tval=document.getElementById("mess").value;   
      var inhtml=document.getElementById("chat_div");
      inhtml.innerHTML=inhtml.innerHTML+"<p class='me'>Me:-"+tval+"</p>";
      inhtml.innerHTML=inhtml.innerHTML+"<p class='demo'>Demo:-Hi! how are you</p>";
      audio.play();
    }
    

    this code is from talkerscode For complete tutorial visit http://talkerscode.com/webtricks/play-sound-on-notification-using-javascript-and-php.php

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

    As of 2016, the following will suffice (you don't even need to embed):

    let src = 'https://file-examples.com/wp-content/uploads/2017/11/file_example_MP3_700KB.mp3';
    let audio = new Audio(src);
    audio.play();
    

    See more here.

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

    One more plugin, to play notification sounds on websites: Ion.Sound

    • Basic Demo
    • Advanced Demo
    • Ion.Sound GitHub Page

    Advantages:

    • JavaScript-plugin for playing sounds based on Web Audio API with fallback to HTML5 Audio.
    • Plugin is working on most popular desktop and mobile browsers and can be used everywhere, from common web sites to browser games.
    • Audio-sprites support included.
    • No dependecies (jQuery not required).
    • 25 free sounds included.

    Set up plugin:

    // set up config
    ion.sound({
        sounds: [
            {
                name: "my_cool_sound"
            },
            {
                name: "notify_sound",
                volume: 0.2
            },
            {
                name: "alert_sound",
                volume: 0.3,
                preload: false
            }
        ],
        volume: 0.5,
        path: "sounds/",
        preload: true
    });
    
    // And play sound!
    ion.sound.play("my_cool_sound");
    
    0 讨论(0)
  • 2020-11-28 02:13

    Play cross browser compatible notifications

    As adviced by @Tim Tisdall from this post , Check Howler.js Plugin.

    Browsers like chrome disables javascript execution when minimized or inactive for performance improvements. But This plays notification sounds even if browser is inactive or minimized by the user.

      var sound =new Howl({
                         src: ['../sounds/rings.mp3','../sounds/rings.wav','../sounds/rings.ogg',
                               '../sounds/rings.aiff'],
                         autoplay: true,
                         loop: true
                        });
    
                   sound.play();
    

    Hope helps someone.

    0 讨论(0)
提交回复
热议问题