100

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

TylerH
  • 19,065
  • 49
  • 65
  • 86
SeBa
  • 1,001
  • 2
  • 7
  • 4
  • You might be able to find some help here: http://stackoverflow.com/questions/41360490/how-to-make-html-video-autoplay-on-phones-and-tablets/ Personally, I have yet to get any video at all to autoplay on an iphone, even after following all those tips and Apples policies. – Mark Apr 23 '17 at 17:39
  • It took me hours to figure out. To Try to save hours of everyone else I've summed up my findings in a blog. Hope it helps. https://medium.com/@BoltAssaults/autoplay-muted-html5-video-safari-ios-10-in-react-673ae50ba1f5 – BoltCoder Jul 03 '20 at 17:03

4 Answers4

267

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/

Jee Mok
  • 4,537
  • 7
  • 35
  • 66
Pete Florence
  • 2,823
  • 1
  • 8
  • 8
74

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

halfer
  • 18,701
  • 13
  • 79
  • 158
Dilip Godhani
  • 1,528
  • 2
  • 15
  • 31
  • 2
    great tip but i have a question about this that I submitted : https://stackoverflow.com/questions/50400902/detect-if-ios11-device-is-in-low-power-mode-to-prevent-bad-ux-on-normally-correc – Mathieu May 17 '18 at 21:44
  • 9
    I spent the last hour or so trying to understand why my videos were not playing automatically. Thank you for this! – lior Oct 16 '18 at 02:23
  • 2
    Thank you for this! – Nikita Rogatnev Nov 21 '18 at 15:25
  • 5
    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 – Muhammad Osama Jan 04 '19 at 05: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 09 '19 at 15:19
11

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

Jee Mok
  • 4,537
  • 7
  • 35
  • 66
Shakti
  • 496
  • 5
  • 10
0

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+)

maxrojas
  • 41
  • 5