Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you play a sound on the web browser?

How do I play a sound on the web browser as notification?

like image 367
never_had_a_name Avatar asked Jan 22 '23 10:01

never_had_a_name


1 Answers

You can use the <audio> tag combined with JavaScript to play sounds at a given time. You'll need JavaScript, of course, as it's done on the frontend, and hence, with client-side programming.

For example,

<audio style="display: none;" id="notification" preload src="path/to/soundfile">

Then, for the scripting, place this somewhere in any part of your script that requires sound notification to occur:

document.getElementById('notification').play();

For those who recommend Flash as it's supported in IE, note graceful degradation, where, for non-essential things (such as sound notification) we choose to use new, recommended technologies that work on most browsers, instead of using hackish, insecure methods to try to get all browsers to work.

like image 142
Delan Azabani Avatar answered Jan 30 '23 01:01

Delan Azabani