Video HTML

6 min read

How to Autoplay Videos in HTML?

This blog will start by explaining what HTML video autoplay is, its usage, and then the code snippet for implementing it. We will also cover how to use it in different scenarios and browser compatibility. Lastly, we will discuss how to fix HTML Video Autoplay not working error.

Feature image
How to Autoplay Videos in HTML?
Hello from Gumlet! We have an ad-free video player you can deeply customize without a single line of code. Also, set up autoplay and loop with a single click. With our player, your videos kick off faster than a cheetah on a caffeine high, keeping your audience glued to their screens. We also have a fully stacked video hosting solution to go with it. Level up your video streaming game with Gumlet, Signup now, it's free.

HTML Video Autoplay - What it means?

HTML Video Autoplay is a relatively new feature in HTML5. It allows browsers to start playing a video automatically without requiring any trigger or interaction from the user. This can be achieved by adding the “autoplay” attribute, which is a boolean attribute, to the HTML video element. Although helpful in some avenues, this feature may often lead to negative user experiences. As such, most browsers and mobile devices have disabled the autoplay feature.

There is a workaround for this, however, as most browsers allow video autoplay under the following circumstances:

  • The video is muted.
  • The user has interacted with the site by clicking, tapping, or engaging with similar buttons.
A video platform with everything at your fingertips

Code Snippet for HTML Video Autoplay

To understand how the video autoplay attribute works, let us look at the code required to run this feature:‌

<body>
    <center>
        <h3>html video autoplay</h3>
        <video controls width="500" autoplay muted>
            <source src="birds.mp4" type="video/mp4">
            <source src="birds.ogg" type="video/ogg">
            <p>Sorry, Your Browser Doesn't Support Embedded Videos. Here's the <a href="https://www.youtube.com/watch?v=yDzQDeaW6O8">Link to the video</a></p>
        </video>
    </center>
</body>

Let us understand the code's functionality by breaking it into different tags.

  • <Video> Tag: The <video> tag is used initially to embed video on the relevant webpage.
  • Autoplay Attribute: Autoplay is a boolean attribute that can be added to compel the browser to play the video automatically as soon as possible without waiting for the page to finish loading the data. With the autoplay attribute, the video will start playing as soon as the page loads and will be muted unless the user decides to unmute it manually.
  • Controls Attribute: The control attribute allows users to gain control over how the video is played. If it's present, users can tinker with playback settings like volume, seeking, pause/resume option, etc.
  • Width Attribute: The width attribute is present to set the dimensions and size of the video to be played on the webpage.
  • <Source> Tag: The source tag, alongside src attributes, determine the file path of the video to be played.
  • Type Attribute: The Type attribute specifies the video format, like MP4, ogg, WebM, or any other format.

The video will start playing without any sound/volume if the video is playable. But if it is not playable, an error text will be sent to the viewers.

Browser Compatibility

Here is a list of the earliest browser versions that started supporting the video autoplay feature:‌

BrowserChromeEdgeFirefoxInternet ExplorerOperaSafari
Version493.5910.53.1

However, it’s imperative to understand that the autoplay feature will render differently on different web browsers.

For example, some browsers may require the video to be muted before the video starts playing automatically. In others, the users must engage with the page before automatic video playback occurs.

Different Cases for HTML Video Autoplay

In different cases, specific attributes can be used to change the functionality and behavior of the autoplay feature.

Muted Autoplay

Muted autoplay is a feature that allows browsers to support automatic video playback without sound. The user will have to enable the audio manually. With muted autoplay, browsers can provide appropriate visual appeal without disrupting the user experience.

Code Snippet:

<video controls width=”500” autoplay muted> 
	<source src=”birds.mp4” type=”video/mp4”> 
</video>

Looped Autoplay

The looped attribute compels the browser to keep playing the video indefinitely in a loop unless the user pauses the video manually. This attribute is useful for a short promotional video that must be displayed on a particular web page.

Code Snippet:

<video width="420" controls autoplay muted loop>
        <source src="sample.mp4" type="video/mp4">
</video>

Looped Autoplay Without Controls

Autoplay and loop attributes can be used together in case of silent animations on a website. In such a case, users cannot control the video playback.

Code Snippet:

<video width="420" autoplay muted loop>
        <source src=" sample.mp4" type="video/mp4">
</video>

Check if your video is optimized for instant start times on auto-play

Styling Video Autoplay with CSS

To increase the aesthetic appeal of the video, different CSS elements can be incorporated to alter the video's dimensions and make other adjustments like adding backdrop colors and blurring effects.

Code Snippet:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>HTML Fullscreen Video Autoplay </title>
    <link rel="stylesheet" href="css/style.css">
</head>
<body>
    <center>
        <div class="main"></div>
        <video id="wing" autoplay muted loop>
            <source src="birds.mp4" type="video/mp4">
            <source src="birds.ogg" type="video/ogg">
            Your browser does not support the video tag.
        </video>
    </center>
</body>
</html>

Autoplay YouTube Video with iFrame Tag in HTML

In the case of YouTube content, the video tag cannot be used as YouTube does not allow users to access its raw video files. So, you will have to work with a unique video ID that does not match the actual file. As such, the element must be used.

Code Snippet:

<!DOCTYPE html>
<html>
<body>
    <center>
        <h3>Youtube video autoplay</h3>
        <iframe width="500px" src="https://www.youtube.com/embed/xpZ0Nq5WSQQ?autoplay=1&mute=1">
        </iframe>
    </center>
</body>
</html>

Usages

Video autoplay is a powerful tool for the following:

  • Displaying Promotional Videos: Websites can use the autoplay feature to automatically display advertisements to capture the user’s attention and convey information about specific products and services.
  • News & Media Outlets: Autoplay feature is widely used on media websites to display the latest news and information without requiring any prompt from the users.
  • Educational Materials: Many ed-tech websites use the video autoplay feature to start the automatic playback of important video content such as tutorials, lectures, and pre-recorded classes.
  • Background Videos: Some websites use videos as backgrounds to increase the aesthetic appeal of their web pages. The video autoplay feature is often used to facilitate such background videos.

How to Fix HTML Video Autoplay Not Working?

In some cases, the video autoplay feature does not work. The most common root of this problem lies in the differing policies of different browsers. Some browsers only enable the autoplay feature when the volume is set to mute. So, ensure you have added the "muted" attribute next to the "autoplay" attribute.

Also, many developers make the mistake of using the autoplay attribute with the following syntax:‌

autoplay=" true"

Despite being a boolean attribute, this syntax will only work when the video is set to mute or has no original audio of its own.

This is the primary fix that helped this problem for most users. However, if you're using iOS Safari, the syntaxes mentioned above won't work. You will have to add another attribute called 'playsinline'.

Here's how:‌

<video width="420" autoplay muted playsinline>
        <source src=" sample.mp4" type="video/mp4">
</video>

Note: It's generally recommended to use the HTML video autoplay instead of JavaScript, as the script could lead to displaying fallback content in case of errors.

Conclusion

In conclusion, auto-playing videos can enhance the user experience on your website, especially if you have a lot of multimedia content to share. However, it's important to remember that auto-playing videos can also be distracting and annoying if not implemented correctly. By following the steps outlined in this blog, you can easily add autoplay functionality to your videos in HTML. Remember always to provide a way for users to control the video playback and to consider the impact on page load times and user data usage.

Frequently Asked Questions (FAQs)

1. How to disable video autoplay in HTML?

The only way to disable the video autoplay in HTML is to remove the attribute altogether. The command autoplay=”false” won’t work, and the video will continue to be displayed automatically unless the attribute is completely removed from the code.

2. How to autoplay video with sound in HTML?

You can autoplay a video with sound in HTML by using the “Autoplay” attribute without using the “muted” attribute inside the <video> tag. However, many browsers do not support the automatic playback of unmuted videos.

3. Why is HTML video autoplay not working on mobile?

Mobile devices have generally disabled the use of video autoplay to prevent unwanted data usage and optimize user experience by preventing interruptions.

4. How do I autoplay a video in Chrome HTML?

You can easily autoplay a video in Chrome HTML by including the “autoplay muted” attribute in the <video> tag.

Reduce bounce rate by 74%

Get the fastest loading images and videos possible for your websites with Gumlet.