Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Playing a sound in a browser (Chrome), from javascript

I am writing an html page. I want it to make sounds, as specified by some javascript as it runs.

In the html, as recommended in the answers I have read here, I have the line

<embed src="wavs/beep.wav" autostart="true" width="0" height="0" id="beep" 
  enablejavascript="true">

This plays the sound at load time, so I am confident I have given a valid path to a valid .wav file. (I will set autostart to false once everything is working.)

I have a function

function playSound ( soundname )   
  {
    var thissound = document.getElementById( soundname );
    thissound.Play();
    alert( "Played " + soundname );
  }

which I call using

  playSound( "beep" );

But when that call is made, there is no sound, although the alert happens. It looks to me as if I am doing everything in the recommended way, but I must have got something wrong. What should I check next?

like image 480
user36956 Avatar asked Sep 11 '13 11:09

user36956


People also ask

How do I play sound through my browser?

Ensure the Allow sites to play sound (recommended) option is enabled in the Chrome sound settings (chrome://settings/content/sound.) Make sure the page is not muted by right-clicking on its tab. If it's muted, you'll see an Unmute site option.

Can JavaScript make sounds?

To beep a sound on a button click using JavaScript, it does not require a lengthy code to implement. However, you need to embed the source of the beeping music using the HTML tab and provide the link of the audio in src.


3 Answers

Use the native HTML5 Audio API

In Chrome's console try creating a new instance of the Audio element, passing in the path of the sound you want to buffer. For example, if we want a typewriter sound we can do something like:

const typeWriter = new Audio("https://www.freesound.org/data/previews/256/256458_4772965-lq.mp3");

Note: for this exact sound to buffer and avoid a CSRF issue, you must be working in a console at www.freesound.org

typeWriter now represents an html audio tag.

Try making the sound loop by setting typeWriter.loop = true

Finally, play/pause the sound with typeWriter.play() & typeWriter.pause()

like image 160
IliasT Avatar answered Nov 07 '22 18:11

IliasT


Have tried this code, and it works well in most browsers used the js method below

function playSound(soundfile_ogg, soundfile_mp, soundfile_ma) {
    if ("Audio" in window) {
        var a = new Audio();
        if (!!(a.canPlayType && a.canPlayType('audio/ogg; codecs="vorbis"')
                .replace(/no/, '')))
            a.src = soundfile_ogg;
        else if (!!(a.canPlayType && a.canPlayType('audio/mpeg;').replace(/no/,
                '')))
            a.src = soundfile_mp;
        else if (!!(a.canPlayType && a.canPlayType(
                'audio/mp4; codecs="mp4a.40.2"').replace(/no/, '')))
            a.src = soundfile_ma;
        else
            a.src = soundfile_mp;

        a.autoplay = true;
        return;
    }
}
like image 26
Nosakhare Belvi Avatar answered Nov 07 '22 19:11

Nosakhare Belvi


You are getting an error because the getElementById gives you an embed element and not an audio element. embed element does not know how to play. In other words, there is no play method in embed element. Use the following code to play it properly.

<html>
<script>
function playSound ( soundname )
  {
    var thissound = document.getElementById( soundname );
    thissound.play();
    alert( "Played " + soundname );
  }
</script>
<body>
  <audio src="wavs/beep.wav" id="beep" autostart="false"></audio>
  <input type=button onclick="playSound('beep')">play</input>
  </body>
  </html>

One more note. Depending on the browsers you might want to support, you need multiple versions of the audio sources defined. Refer to this for details.

like image 38
Crusher Avatar answered Nov 07 '22 18:11

Crusher