Video auto play is not working in Safari and Chrome desktop browser

JavascriptHtmlGoogle ChromeSafariWebkit

Javascript Problem Overview


I spent quite a lot of time trying to figure out why video embedded like here:

<video height="256" loop autoplay muted controls id="vid">
    	 <source type="video/mp4" src="video_file.mp4"></source>
         <source type="video/ogg" src="video_file.ogg"></source>
</video>

starts playing automatically once the page is loaded in FireFox but cannot do autoplay in Webkit based browsers. This only happened on some random pages. So far I was unable to find the cause. I suspect some unclosed tags or extensive JS created by CMS editors.

Javascript Solutions


Solution 1 - Javascript

The best fix I could get was adding this code just after the </video>

<script>
	document.getElementById('vid').play();
</script>

...not pretty but somehow works.

UPDATE Recently many browsers can only autoplay the videos with sound off, so you'll need to add muted attribute to the video tag too

<video autoplay muted>
...
</video>

Solution 2 - Javascript

After using jQuery play() or DOM maniupulation as suggested by the other answers, it was not still working (Video wasn't autoplaying) in the Chrome for Android (Version 56.0).

As per this post in developers.google.com, From Chrome 53, the autoplay option is respected by the browser, if the video is muted.

So using autoplay muted attributes in video tag enables the video to be autoplayed in Chrome browsers from version 53.

Excerpt from the above link:

> Muted autoplay for video is supported by Chrome for Android as of version 53. Playback will start automatically for a video element once it comes into view if both autoplay and muted are set[...] > > > > > - Muted autoplay is supported by Safari on iOS 10 and later. > - Autoplay, whether muted or not, is already supported on Android by Firefox and UC Browser: they do not block any kind of autoplay.

Solution 3 - Javascript

>Google just changed their policy for autoplay videos, it has to be muted

You can check here

so just add muted

<video height="256" loop="true" autoplay="autoplay" controls="controls" id="vid" muted>
         <source type="video/mp4" src="video_file.mp4"></source>
         <source type="video/ogg" src="video_file.ogg"></source>
</video>

Solution 4 - Javascript

It happens that Safari and Chrome on Desktop do not like DOM manipulation around the video tag. They will not fire the play order when the autoplay attribute is set even if the canplaythrough event has fired when the DOM around the video tag has changed after initial page load. Basically I had the same issue until I deleted a .wrap() jQuery around the video tag and after that it autoplayed as expected.

Solution 5 - Javascript

For me the issue was that the muted attribute needed to be added within the video tag. I.e.:

<video width="1920" height="1980" src="video/Night.mp4"
type="video/mp4" frameborder="0" allowfullscreen autoplay loop
muted></video>`

Solution 6 - Javascript

Chrome does not allow to auto play video with sound on, so make sure to add muted attribute to the video tag like this

<video width="320" height="240"  autoplay muted>
  <source src="video.mp4" type="video/mp4">
</video>

Solution 7 - Javascript

var video = document.querySelector('video');
video.muted = true;
video.play()

Only this solution helped me, <video autoplay muted ...>...</video> didn't work...

Solution 8 - Javascript

I've just get now the same issue with my video

<video preload="none" autoplay="autoplay" loop="loop">
  <source src="Home_Teaser.mp4" type="video/mp4">
  <source src="Home_Teaser" type="video/webm">
  <source src="Home_Teaser.ogv" type="video/ogg">
</video>

After search, I've found a solution:

If I set "preload" attributes to "true" the video start normally

Solution 9 - Javascript

It worked for me when combined with muted attribute

Solution 10 - Javascript

> Try this:

  <video width="320" height="240"  autoplay muted>
            <source src="video.mp4" type="video/mp4">
  </video>

Solution 11 - Javascript

Adding the below code at the bottom of the page worked for me . I dont know why it works :(

 setTimeout(function(){
     document.getElementById('vid').play();
 },1000);

Solution 12 - Javascript

On safari iPhone when battery is low and iPhone is on Low Power Mode it won`t autoplay, even if you have the following attributes: autoplay, loop, muted, playsinline set on your video html tag.

Walk around I found working is to have user gesture event to trigger video play:

document.body.addEventListener("touchstart", function () {
    var allVideos = document.querySelectorAll('video');
    for (var i = 0; i < allVideos.length; i++) {
        allVideos[i].play();
    }
},{ once: true });

You can read more about user gesture and Video Policies for iOS in webkit site:

https://webkit.org/blog/6784/new-video-policies-for-ios/

Solution 13 - Javascript

We recently addressed a similar issue with an embedded video and found that the autoplay and muted attributes were not sufficient for our implementation.

We added a third "playsinline" attribute to the code and it fixed the issue for iOS users.

This fix is specific to videos that are to be played inline. From https://webkit.org/blog/6784/new-video-policies-for-ios/ :

On iPhone,

Solution 14 - Javascript

Google updated Autoplay Policy. Autoplay only work on mute mode. Check the link https://developers.google.com/web/updates/2017/09/autoplay-policy-changes

Solution 15 - Javascript

Try swapping in autoPlay for autoplay.

It seems to be case sensitive at times. Very bizarre because it worked as autoplay for me, but only if I included controls

Solution 16 - Javascript

  • Please use muted keyword before autoplay word, Here some privacy change at April, 2018.

  • You may read policy here

Solution 17 - Javascript

I had a problem when playing a video on Safari on iPhones. Adding the playsinline attribute in the video tag can solve this problem, and it works!

<video autoplay muted loop playsinline class="someClass">
  <source src="source.mp4" type="video/mp4">
</video>

You will also get this problem on Safari on OSX, in case you get yourself confused about this property playsinline, here is the explaination.

> Mobile browsers, playsinline will play the video right where it is instead of the default, which is to open it up fullscreen while it plays.

For the Safari on OSX, as the default websites Auto-Play option is Stop Media with Sound, this strategy can also introduce the permission issue.

That's why we need the property muted.

Safari Preferences

Solution 18 - Javascript

I got mine to autoplay by making it muted. I think Google rules won't let chrome auto-play unless it's muted.

<video id="video" controls autoplay muted
        border:0px solid black;"
        width="300"
        height="300">
    <source src="~/Videos/Lumen5_CTAS_Home2.mp4"
            type="video/mp4" />
    Your browser does not support the video tag.
    Please download the mp4 plugin to see the CTAS Intro.
</video>

Solution 19 - Javascript

For angular, you'll have to mute it and play it in the ngAfterViewInit() like the following

<video height="256" loop autoplay muted controls id="vid" #videoRef>
         <source type="video/mp4" src="video_file.mp4"></source>
         <source type="video/ogg" src="video_file.ogg"></source>
</video>
 ​@ViewChild('videoRef', { static: true }) videoRef!: ElementRef

​ngAfterViewInit(): void {
  ​const media = this.videoRef.nativeElement
  ​media.muted = true 
  ​media.play() 
​ } 

Solution 20 - Javascript

None of the other answers worked for me. My workaround was to trigger a click on the video itself; hacky (because of the timeout that is needed) but it works fine:

function startVideoIfNotStarted () {
    $(".id_of_video_tag").ready(function () {
        window.setTimeout(function(){
            videojs("id_of_video_tag").play()
        }, 1000);
    });
}
$(startVideoIfNotStarted);

Solution 21 - Javascript

Spent two hours trying all solutions mentioned above.

This is what finally worked for me:

var vid = document.getElementById("myVideo");
vid.muted = true;

Solution 22 - Javascript

Angular 10:

<video [muted]="true" [autoplay]="true" [loop]="true">
    <source src="/assets/video.mp4" type="video/mp4"/>
</video>

Solution 23 - Javascript

Try this:

    <video height="256" loop autoplay controls id="vid">
     <source type="video/mp4" src="video_file.mp4"></source>
     <source type="video/ogg" src="video_file.ogg"></source>

This is how I normally do it. loop, controls and autoplay do not require a value they are boolean attributes.

Solution 24 - Javascript

This is because of now chrome is preventing auto play in html5 video, so by default they will not allow auto play. so we can change this settings using chrome flag settings. this is not possible for normal case so i have find another solution. this is working perfect... (add preload="auto")

<video autoplay preload="auto" loop="loop" muted="muted" id="videoBanner" class="videoBanner">
<source src="banner-video.webm" type="video/webm">
<source src="banner-video.mp4" type="video/mp4">
<source src="banner-video.ogg" type="video/ogg">

var herovide = document.getElementById('videoBanner');
       herovide.autoplay=true;
       herovide.load();  

Solution 25 - Javascript

<video onload='this.play()' src='the source' autoplay controls></video>

This worked for me.

Solution 26 - Javascript

I had a case where it had something to do with the order of the different filetypes. Try to change it and see if that helps.

Solution 27 - Javascript

I started out with playing all the visible videos, but old phones weren't performing well. So right now I play the one video that's closest to the center of the window and pause the rest. Vanilla JS. You can pick which algorithm you prefer.

//slowLooper(playAllVisibleVideos);
slowLooper(playVideoClosestToCenter);

function isVideoPlaying(elem) {
    if (elem.paused || elem.ended || elem.readyState < 2) {
        return false;
    } else {
        return true;
    }
}
function isScrolledIntoView(el) {
    var elementTop = el.getBoundingClientRect().top;
    var elementBottom = el.getBoundingClientRect().bottom;
    var isVisible = elementTop < window.innerHeight && elementBottom >= 0;
    return isVisible;
}
function playVideoClosestToCenter() {
    var vids = document.querySelectorAll('video');
    var smallestDistance = null;
    var smallestDistanceI = null;
    for (var i = 0; i < vids.length; i++) {
        var el = vids[i];
        var elementTop = el.getBoundingClientRect().top;
        var elementBottom = el.getBoundingClientRect().bottom;
        var elementCenter = (elementBottom + elementTop) / 2.0;
        var windowCenter = window.innerHeight / 2.0;
        var distance = Math.abs(windowCenter - elementCenter);
        if (smallestDistance === null || distance < smallestDistance) {
            smallestDistance = distance;
            smallestDistanceI = i;
        }
    }
    if (smallestDistanceI !== null) {
        vids[smallestDistanceI].play();
        for (var i = 0; i < vids.length; i++) {
            if (i !== smallestDistanceI) {
                vids[i].pause();
            }
        }
    }
}
function playAllVisibleVideos(timestamp) {
    // This fixes autoplay for safari
    var vids = document.querySelectorAll('video');
    for (var i = 0; i < vids.length; i++) {
        if (isVideoPlaying(vids[i]) && !isScrolledIntoView(vids[i])) {
            vids[i].pause();
        }
        if (!isVideoPlaying(vids[i]) && isScrolledIntoView(vids[i])) {
            vids[i].play();
        }
    }
}
function slowLooper(cb) {
    // Throttling requestAnimationFrame to a few fps so we don't waste cpu on this
    // We could have listened to scroll+resize+load events which move elements
    // but that would have been more complicated.
    function repeats() {
        cb();
        setTimeout(function() {
            window.requestAnimationFrame(repeats);
        }, 200);
    }
    repeats();
}

Solution 28 - Javascript

I solved the same problem with,

$(window).on('pageshow',function(){
    var vids = document.querySelectorAll('video');
    for (var i = 0; i < vids.length;){
        vids[i].play();
    }
})

You have to launch the videos after the page has been shown.

Solution 29 - Javascript

Try this it is simple and short and it works with my code whereas I have the video full screen and behind other elements I simply use z-index -1;

    <video autoplay loop id="myVideo">

Solution 30 - Javascript

In React + Chrome, it's better to import the video than give it as src to

import React from 'react';
import styled from 'styled-components';
import video from './videos.mp4';
const StyledVideo = styled.video`
width: 100%;
height: 100vh;
object-fit: cover;
`
const BackgroundVideo = () => {
return (
    <StyledVideo autoPlay loop muted>
        <source src={video} type="video/mp4" />
    </StyledVideo>
);
}

Remember

  • The video is in the same directory, to import it.

  • To autoplay, the video in the background, use autoPlay and muted props are there.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAdam BubelaView Question on Stackoverflow
Solution 1 - JavascriptAdam BubelaView Answer on Stackoverflow
Solution 2 - JavascriptRamValliView Answer on Stackoverflow
Solution 3 - JavascriptmoogaView Answer on Stackoverflow
Solution 4 - JavascriptArnaud LeyderView Answer on Stackoverflow
Solution 5 - JavascriptGarryWView Answer on Stackoverflow
Solution 6 - JavascriptAbdul BasitView Answer on Stackoverflow
Solution 7 - JavascriptAnt0haView Answer on Stackoverflow
Solution 8 - JavascriptThomas LohnerView Answer on Stackoverflow
Solution 9 - JavascriptTasawer KhanView Answer on Stackoverflow
Solution 10 - JavascriptIshan LakshithaView Answer on Stackoverflow
Solution 11 - Javascriptsanath_pView Answer on Stackoverflow
Solution 12 - JavascripttalsibonyView Answer on Stackoverflow
Solution 13 - JavascriptmingalaView Answer on Stackoverflow
Solution 14 - JavascriptBalajiView Answer on Stackoverflow
Solution 15 - JavascriptMikaal NaikView Answer on Stackoverflow
Solution 16 - JavascriptYagnesh bhalalaView Answer on Stackoverflow
Solution 17 - JavascriptekimasView Answer on Stackoverflow
Solution 18 - JavascriptSamView Answer on Stackoverflow
Solution 19 - JavascriptRaphaël BaletView Answer on Stackoverflow
Solution 20 - JavascriptStijn GeukensView Answer on Stackoverflow
Solution 21 - JavascriptdpigeraView Answer on Stackoverflow
Solution 22 - JavascriptАлександр ЛихихView Answer on Stackoverflow
Solution 23 - JavascriptJared.DAGIView Answer on Stackoverflow
Solution 24 - JavascriptRijosh KView Answer on Stackoverflow
Solution 25 - JavascriptSivustonikkariView Answer on Stackoverflow
Solution 26 - JavascriptMoritzGiessmannView Answer on Stackoverflow
Solution 27 - JavascriptubershmekelView Answer on Stackoverflow
Solution 28 - JavascriptCeci Semble Absurde.View Answer on Stackoverflow
Solution 29 - JavascriptCharles NortonView Answer on Stackoverflow
Solution 30 - JavascriptAkarsh SrivastavaView Answer on Stackoverflow