// Tutorial //

How To Add Audio Using HTML5

Published on August 15, 2016 · Updated on September 23, 2020
    Default avatar

    By Alligator.io

    How To Add Audio Using HTML5

    Introduction

    Thanks to the HTML5 audio element, gone are the days of having to use Flash to include audio files on a web page. In this brief tutorial we’ll demonstrate how to add audio to your HTML using the new tag.

    Prerequisites

    Adding Audio to HTML5 Using the <audio> Tag

    This is how you add audio to HTML5 using the <audio> tag:

    index.html
    <audio controls loop autoplay preload="none"
      src="deep-house-track.mp3">
    </audio>
    

    There are a few attributes you can use with the audio element:

    • controls adds controls for play/pause, volume and seeking
    • loop will repeat the playback
    • autoplay plays the audio automatically
    • preload can take the values none, metadata, or auto. The default is auto. It can be good to set it to none on pages where you include multiple audio elements, preventing the browser from downloading all the audio data.

    Format support

    You may want to provide multiple formats because MP3 is not an open format, therefore some browsers can’t support it. A free format, Ogg, would be great across the board, but it’s not supported by all browsers. See here for a list of browser format support. Use the source element within the audio element to provide different formats:

    index.html
    <audio>
      <source src="deep-house-track.ogg">
      <source src="deep-house-track.mp3">
    </audio>
    

    And you can be even more exhaustive by providing a type:

    index.html
    <audio>
      <source src="deep-house-track.ogg"
      type="audio/ogg">
      <source src="deep-house-track.mp3"
      type="audio/mp3">
    </audio>
    

    And you can provide extra markup for browsers that don’t support audio:

    index.html
    <audio>
      <source src="deep-house-track.ogg">
      <source src="deep-house-track.mp3">
      <p>Your browser doesn't support this,
      <a href="">download the file</a>.</p>
    </audio>
    

    This is the basic functionality of the new <audio> element in HTML5.

    Conclusion

    The <audio> element is a significant new feature of HTML. To check for browser support, you can use the Browser Support lookup for <audio> on the Can I Use website

    Or, for a more comprehensive review of the HTML5 markup language, explore our series, How To Build a Website in HTML.

    Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

    Learn more about us


    About the authors
    Default avatar
    Alligator.io

    author



    Still looking for an answer?

    Ask a questionSearch for more help

    Was this helpful?
     
    Leave a comment
    

    This textbox defaults to using Markdown to format your answer.

    You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

    Try DigitalOcean for free

    Click below to sign up and get $200 of credit to try our products over 60 days!

    Sign up

    card icon
    Get our biweekly newsletter

    Sign up for Infrastructure as a Newsletter.

    Sign up
    card icon
    Hollie's Hub for Good

    Working on improving health and education, reducing inequality, and spurring economic growth? We’d like to help.

    Learn more
    card icon
    Become a contributor

    You get paid; we donate to tech nonprofits.

    Learn more
    Welcome to the developer cloud

    DigitalOcean makes it simple to launch in the cloud and scale up as you grow – whether you’re running one virtual machine or ten thousand.

    Learn more ->
    DigitalOcean Cloud Control Panel
    Get started for free

    Sign up and get $200 in credit for your first 60 days with DigitalOcean.

    This promotional offer applies to new account only.

    © 2023 DigitalOcean, LLC.