How to play a notification sound on websites?
Got a minute? Here is the bento box solution. Use <audio>
and JavaScript to trigger a sound:
Just replace "notify.mp3"
with the path to your audio file. Hit that button and Bob's your uncle, you've got your sound!
In-depth explanation
Hold tight. We are going to dive deep into notification sound implementation ensuring broadest browser compatibility and top-notch user experience.
Covering all bases: Cross-browser audio compatibility
Just like a dating app success, you want to appeal to as many (browsers) as you can. So, make sure to offer multiple audio formats:
Catering vintage browsers
If your <audio>
doesn't charm every browser, do a fallback with the <embed>
tag:
Preloading and volume control
Ever been at a gig that starts too loud? Avoid that with volume control. Plus, with sound preloading, your sound is as ready as a coiled spring:
Be a good guest: On-demand initiation
Nobody likes unannounced visitors. The same goes for sound. Initiate playback via user interactions or specific events:
User experience: Handle with care
Lastly, remember usability. Give the user control over the sound, because uncontrolled audio can ruin an otherwise great page.
Advanced topics and considerations
Use a little help from the libraries
Working with raw JavaScript is fun, yet Ion.Sound can make things way easier, and it's independent:
Upgrading embedding: Dynamic JavaScript
The <audio>
element can be created dynamically with JavaScript:
Sneak-a-peek: Invisible audio
Always dreamed of being a sound ninja? Use the JavaScript Audio object to introduce notification sounds with no visible footprint:
Was this article helpful?