164

I have some kind of a strange problem. I try to create a website with a looped background video. The code looks like this one:

<video src="video/bg.mp4" style="z-index: -1;object-fit: cover;" poster="video/bg.jpg" autobuffer autoplay loop muted></video>

This works perfectly fine on most browsers (IE struggles with this object-fit thing but I don't mind) but on iPhone the video won't autoplay but on iPad it does. I already read the New Policies for iOS and I think I meet the requirements (otherwise iPad won't autoplay). I did some other testing:

  • Removing overlaying divs won't fix it
  • Removing z-index won't fix it
  • Wifi or Cellular doesn't make a difference
  • Video filesize doesn't make a difference, too

Am I doing it wrong or does iPhone simply won't autoplay videos and always requires interaction? I only care for iOS 10, I know that the requirements were different on iOS 9

3

7 Answers 7

455

Does playsinline attribute help?

Here's what I have:

<video autoplay loop muted playsinline class="video-background ">
  <source src="videos/intro-video3.mp4" type="video/mp4">
</video>

See the comment on playsinline here: https://webkit.org/blog/6784/new-video-policies-for-ios/

7
  • 47
    playsinline worked for me in conjunction with muted keeping in mind the low power mode quirk on iPhone
    – Ken
    Aug 6, 2018 at 14:38
  • 3
    playsinline Saved the day!!!! Thanks man. BTW, new browser policies demand that if you want to autostart a video, start it muted or you won't be able to do it. +1 to @ken Example for chrome: [developers.google.com/web/updates/2017/09/…
    – Nuno Prata
    Nov 7, 2018 at 19:07
  • 52
    If you're using React, note that the attribute playsinline must be written in camelCase: playsInline. Otherwise it won't work.
    – Quentin D
    Feb 5, 2019 at 10:53
  • 1
    Don't forget to add muted also with playsinline. playsinline attribute won't work alone without muted attribute
    – stojkosd
    Sep 12, 2019 at 15:19
  • 1
    Yes in React or Gatsby, you need to include both 'playsInline' and 'autoPlay' in camelCase. This is a pro tip that will save you hours and trial and error.
    – B-Money
    Jun 15, 2021 at 6:11
131

iOs 10+ allow video autoplay inline. but you have to turn off "Low power mode" on your iPhone.

6
  • 2
    great tip but i have a question about this that I submitted : stackoverflow.com/questions/50400902/…
    – Mathieu
    May 17, 2018 at 21:44
  • 12
    I spent the last hour or so trying to understand why my videos were not playing automatically. Thank you for this!
    – lior
    Oct 16, 2018 at 2:23
  • 2
    Thank you for this! Nov 21, 2018 at 15:25
  • 9
    Its worth mentioning that we can't control the user's device and turn of Low Power Mode. The only thing we can do is prompt the user to "Turn off low power mode" to have a better experience Jan 4, 2019 at 5:52
  • 1
    Happened to me as well and drove me crazy till I found your post. I was already looking at the specs of Safari 11 and 11.1 if they maybe completely disabled autoplay but it was only the low power mode... a devs life can be hard. :-)
    – haeki
    May 9, 2019 at 15:19
13

Here is the little hack to overcome all the struggles you have for video autoplay in a website:

  1. Check video is playing or not.
  2. Trigger video play on event like body click or touch.

Note: Some browsers don't let videos to autoplay unless the user interacts with the device.

So scripts to check whether video is playing is:

Object.defineProperty(HTMLMediaElement.prototype, 'playing', {
get: function () {
    return !!(this.currentTime > 0 && !this.paused && !this.ended && this.readyState > 2);
}});

And then you can simply autoplay the video by attaching event listeners to the body:

$('body').on('click touchstart', function () {
        const videoElement = document.getElementById('home_video');
        if (videoElement.playing) {
            // video is already playing so do nothing
        }
        else {
            // video is not playing
            // so play video now
            videoElement.play();
        }
});

Note: autoplay attribute is very basic which needs to be added to the video tag already other than these scripts.

You can see the working example with code here at this link:

How to autoplay video when the device is in low power mode / data saving mode / safari browser issue

1
  • 2
    But it requires user interaction before the video plays? Since our video is in the stage of the page, we need to ensure that the video plays even if the user is on low-battery-mode and before he interacts with the page. Because it would be pointless to start a video once the user wants to scroll down. Sep 14, 2022 at 9:57
5

I had a similar problem and I tried multiple solution. I solved it implementing 2 considerations.

  1. Using dangerouslySetInnerHtml to embed the <video> code. For example:
<div dangerouslySetInnerHTML={{ __html: `
    <video class="video-js" playsinline autoplay loop muted>
        <source src="../video_path.mp4" type="video/mp4"/>
    </video>`}}
/>
  1. Resizing the video weight. I noticed my iPhone does not autoplay videos over 3 megabytes. So I used an online compressor tool (https://www.mp4compress.com/) to go from 4mb to less than 500kb

Also, thanks to @boltcoder for his guide: Autoplay muted HTML5 video using React on mobile (Safari / iOS 10+)

1
  • Good to know, about the size requirement.
    – Shautieh
    Aug 25, 2022 at 14:11
1

I had the same problem - the video not play on iOS. I tried all the code options "playsinline autoplay loop muted". The problem was the video I received was in the wrong mp4 codec. So what helped us, was to upload the video to Vimeo and download the HD Version again. The video is now playing on all mobile devices.

You could also try to use mpeg streamclip. Here is a screenclip of VLC - those are the correct settings. Hope someone does not have to spend 2 hours searching for the problem - happy holidays

Codec we used

1
  • playsinline worked for me. Thank you for sharing! Dec 29, 2021 at 1:38
1

Sorry, for late answer, but today i found that you need to turn off Battery saving on iPhone to make video autoplay.

-1

Here is a simple solution to auto-play the video in IOS, I've already tried and it is perfectly working on IOS, Android, also on all the browsers on various platforms.

simply use (data-wf-ignore) and (data-object-fit) Attributes for the video and data-wf-ignore for the source tag..

You can see the working example with code here at this Snippet:

<video id="myVideo" autoplay="" muted="" playsinline="" data-wf-ignore="true" data-object-fit="cover">
    <source src="https://www.w3schools.com/html/mov_bbb.mp4" data-wf-ignore="true" />
</video>

1
  • data-wf-ignore is not a standard attribute - what does that refer to? Apr 13, 2022 at 13:01

Not the answer you're looking for? Browse other questions tagged or ask your own question.