Better Programming

Advice for programmers.

Follow publication

Member-only story

Everything You Should Know About the Audio Tag in HTML and JavaScript

Include audio in HTML, and play and pause it in JavaScript

Javascript Jeep🚙💨
Better Programming
Published in
4 min readMay 26, 2020
Audio cheat sheet

The audio tag allows you to add audio to your web pages. The audio tag is introduced in HTML5 .

Adding an audio tag is super easy:

<audio src="/music/meow.mp3"> </audio>

You can also show a message if the browser doesn’t support the audio tag:

<audio src="/music/meow.mp3">    You're browser is Outdated . Please Update The Browser</audio>

Attributes

Controls

The above code alone doesn’t play the music or show anything. We need to add the controls attribute to enable play and pause of the audio. By default, controls is false. You can add enable control of the audio tag by adding either controls or controls = "true”.

<audio src="/music/meow.mp3" controls>    You're browser is Outdated . Please Update The Browser</audio>

Don’t forget to add controls; otherwise, nothing will be displayed.

Multiple audio sources

Create an account to read the full story.

The author made this story available to Medium members only.
If you’re new to Medium, create a new account to read this story on us.

Or, continue in mobile web

Already have an account? Sign in

Responses (2)

Write a response