Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Play a beep sound on button click

OK I've read several answers here but they didn't help me at all (in fact, none of them is being accepted as answer)

Question is how to "Play a beep sound" on "button click"

I am trying to make a website that works on touchscreen device so I want every button click events will play a beep sound, that should be nicer for users who using the website. Beep sound file is here: http://www.soundjay.com/button/beep-07.wav . I only need this work on Google Chrome (supports HTML5)

I understand this need to work on client-side so I tried this:

Javascript:

<script>
    function PlaySound(soundObj) {
        var sound = document.getElementById(soundObj);
        sound.Play();
    }
</script>

HTML

<embed src="/beep.wav" autostart="false" type="audio/mpeg" loop="false" width="0" height="0" id="beep" enablejavascript="true" />
<asp:LinkButton ID="lbtnExit" runat="server" OnClick="lbtnExit_Click" OnClientClick="PlaySound('beep')" CssClass="btn btn-lg btn-danger" Text="Exit <i class='fa fa-sign-out' style='font-size: 40px'></i>"></asp:LinkButton>

But it doesn't work, nothing happens when I click the button.

like image 354
Ronaldinho Learn Coding Avatar asked Apr 10 '15 17:04

Ronaldinho Learn Coding


People also ask

How do you add a beeping sound in HTML?

You'll need to embed a short WAV file in the HTML, and then play that via code. You would then call it from JavaScript code as such: PlaySound("sound1"); This should do exactly what you want - you'll just need to find/create the beep sound yourself, which should be trivial.


5 Answers

You could use an audio tag like this:

    <audio id="audio" src="http://www.soundjay.com/button/beep-07.wav" autoplay="false" ></audio>
    <a onclick="playSound();"> Play</a>
    <script>
    function playSound() {
          var sound = document.getElementById("audio");
          sound.play();
      }
    </script>

Here is a Plunker

like image 165
netrevisanto Avatar answered Oct 01 '22 22:10

netrevisanto


Admitting you already have something like <div id='btn'>Click to play!</div> in your html, you could do it as simple as:

$('#btn').click( () => new Audio('mp3/audio.mp3').play() );

This is the best IMO because it allow riffle clicking on the button (which is not possible in other answers at the time) and is a one liner.

WORKING DEMO

like image 36
TOPKAT Avatar answered Oct 01 '22 21:10

TOPKAT


This works fine

function playSound () {
    document.getElementById('play').play();
}
<audio id="play" src="http://www.soundjay.com/button/beep-07.wav"></audio>

<button onclick="playSound()">Play</button>
like image 40
Downgoat Avatar answered Oct 01 '22 22:10

Downgoat


Technically, the following doesn't answer the question about "playing" a beep, but if asking how to "generate" a beep, then consider the following code that I found on this website:

a=new AudioContext()
function beep(vol, freq, duration){
  v=a.createOscillator()
  u=a.createGain()
  v.connect(u)
  v.frequency.value=freq
  v.type="square"
  u.connect(a.destination)
  u.gain.value=vol*0.01
  v.start(a.currentTime)
  v.stop(a.currentTime+duration*0.001)
}

Sample values for the call: beep(20, 100, 30). The aforementioned website includes more details and sound samples.

The sound can be in response to a button click or programmatically generated at will. I have used it in Chrome but have not tried it in other browsers.

like image 22
Alan M. Avatar answered Oct 01 '22 21:10

Alan M.


With raw JavaScript, you can simply call:

new Audio('sound.wav').play()
like image 39
Joe Iddon Avatar answered Oct 01 '22 21:10

Joe Iddon