Home > Article > Web Front-end > How to Emit a Beeping Sound in JavaScript?
How can JavaScript emit a beeping sound?
If a user exceeds the character limit specified by the
Using Raw Audio
You can link an external audio file (.wav or .mp3) containing the beep sound to your HTML using the
<code class="html"><audio id="beep"> <source src="beep.wav" type="audio/wav"> </audio></code>
Then, in your JavaScript, you can utilize the play() method of the beep audio element to trigger the sound.
Using Base64 Encoded Data URI
Modern browsers support loading audio files using base64-encoded data URIs. Here's an example:
<code class="html"><audio id="beep"> <source src="data:audio/wav;base64,//...removed for brevity..." type="audio/wav"> </audio></code>
Replace the ...removed for brevity... section with the base64-encoded representation of your beep sound file.
Other Options
Alternatively, you could use a JavaScript library like Howler.js, which simplifies audio playback in the browser. Check the documentation for details on its usage.
Compatibility
Note that data URIs may not be supported in older browsers, so it's advisable to use them with caution.
The above is the detailed content of How to Emit a Beeping Sound in JavaScript?. For more information, please follow other related articles on the PHP Chinese website!