How to embed audio element in a HTML document ?

Last Updated : 30 Sep, 2020

Since the release of HTML5, audio can be added to webpages using the <audio> tag. Previously audio could be only played on webpages using web plugins like Flash. The <audio> tag is an inline element which is used to embed sound files into a web page. It is a very useful tag if you want to add audio such as songs, interviews, etc on your webpage.

Syntax:

<audio>
    <source src="sample.mp3" type="audio/mpeg">
</audio>

Example 1:

HTML
<!DOCTYPE html>
<html>

<body>
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>

    <h3>
        How to embed audio in a HTML document?
    </h3>

    <audio controls>
        <source src="test.mp3" type="audio/mp3">
        <source src="test.ogg" type="audio/ogg">
    </audio>

</body>

</html>

Output:

Example 2: In this example, audio will play automatically after loading the web page.

HTML
<!DOCTYPE html>
<html>

<body>
    <h1 style="color:green;">
        GeeksforGeeks
    </h1>

    <h3>
        How to embed audio in a HTML document?
    </h3>

    <audio controls autoplay>
        <source src="test.mp3" type="audio/mp3">
        <source src="test.ogg" type="audio/ogg">
    </audio>

</body>

</html>

Output:

Comment