Media Players
<source> elements are used inside <audio> or <video> tags to define multiple media sources. The browser picks the first compatible source it can play.
Basic Syntax:
<audio controls>
<source src="audio.mp3" type="audio/mpeg">
<source src="audio.ogg" type="audio/ogg">
Your browser does not support the audio element.
</audio>How It Works:
The browser reads each
<source>tag in order.It checks whether it can play the
type(MIME type, e.g.,audio/mpeg).It plays the first one it supports.
If none match, it shows the fallback text (e.g., "Your browser does not support...").
Why Use <source>:
<source>:To offer multiple formats (e.g.,
.mp3,.ogg) for better compatibility across browsers.To support different bitrates or qualities.
Optional type:
type:You can omit type, but specifying it helps the browser skip unsupported formats faster.
Example (video):
Important Notes:
Only the first playable source is used.
<source>does not fallback like<img>'ssrcset; it's sequential testing.You canβt use
<source>without<audio>or<video>.
Here are good free audio player libraries with nice UIs:
UI: Clean, modern, skinnable
Features: HTML5 fallback, playlist support, accessibility
Install:
Plyr
https://github.com/sampotts/plyr
2. Plyr
UI: Sleek, modern, customizable
Features: Audio & video support, keyboard accessibility, responsive
Install:
https://codepen.io/memetican/pen/emppoKg/2889ed3672715e250c2ace16263362ee
Howler.js
https://github.com/goldfire/howler.js/blob/master/examples/sprite/index.html
UI: None (audio logic only)
Use case: Build your own custom UI with full playback control
Install:
Recommendation: Use Plyr if you want an attractive UI out-of-the-box and minimal setup. Use MediaElement.js for broader compatibility and fallback support. Use Howler.js only if building a fully custom UI.
Last updated