HTMLAudioElement
Audio Element
Represents an <audio> tag. Shares almost all properties with <video> — the key difference is there's no visual output.
<audio
src="song.mp3"
controls
></audio>
Key Properties
| Property | Type | Description |
|---|---|---|
| src | string | URL of the audio file. |
| volume | number | 0.0 (silent) to 1.0 (full volume). |
| muted | boolean | Mute/unmute without changing the volume property. |
| loop | boolean | If true, audio restarts automatically when it ends. |
| currentTime | number | Playback position in seconds. Set it to seek. |
| paused | boolean | True if not playing. Read-only. |
Common Methods
| Method | Returns | Description |
|---|---|---|
| play() | Promise | Start playback. Returns a Promise. |
| pause() | void | Pause playback. |
Quick Examples
JS
// Play a sound effect on button click (no HTML audio element needed)
const click = new Audio('click.mp3');
document.querySelector('button').addEventListener('click', () => {
click.currentTime = 0; // rewind so it plays even if still playing
click.play();
});
// Background music with volume control
const music = document.querySelector('audio');
music.loop = true;
music.volume = 0.4;
music.play();
document.querySelector('#vol-slider').addEventListener('input', (e) => {
music.volume = e.target.value / 100;
});Related Elements
Related Properties
Related Methods