HTML5 Video autoplay on iPhone

167,244

Solution 1

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/

Solution 2

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

Solution 3

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

Solution 4

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

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

Share:
167,244

Related videos on Youtube

SeBa
Author by

SeBa

Updated on December 24, 2021

Comments

  • SeBa
    SeBa over 2 years

    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

  • Mathieu
    Mathieu almost 6 years
    great tip but i have a question about this that I submitted : stackoverflow.com/questions/50400902/…
  • tolmark
    tolmark almost 6 years
    Halleluja! Thank you for this, had tried about a dozen other solutions to no avail.
  • Ken
    Ken almost 6 years
    playsinline worked for me in conjunction with muted keeping in mind the low power mode quirk on iPhone
  • JCraine
    JCraine over 5 years
    This is the answer.
  • lior
    lior over 5 years
    I spent the last hour or so trying to understand why my videos were not playing automatically. Thank you for this!
  • Nuno Prata
    Nuno Prata over 5 years
    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/…
  • Nikita Rogatnev
    Nikita Rogatnev over 5 years
    Thank you for this!
  • Muhammad Osama
    Muhammad Osama over 5 years
    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
  • Gabbr Issimo
    Gabbr Issimo over 5 years
    I'm not joking, you saved my life
  • Quentin D
    Quentin D over 5 years
    If you're using React, note that the attribute playsinline must be written in camelCase: playsInline. Otherwise it won't work.
  • haeki
    haeki about 5 years
    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. :-)
  • stojkosd
    stojkosd over 4 years
    Don't forget to add muted also with playsinline. playsinline attribute won't work alone without muted attribute
  • Zankar
    Zankar over 4 years
    This works - thank you! Just another iOS exception to worry about :D
  • Hugo Nogueira
    Hugo Nogueira over 4 years
    @QuentinD you saved my life. Now working in all devices including iOS.
  • B-Money
    B-Money almost 3 years
    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.
  • Levon Grigoryan
    Levon Grigoryan almost 3 years
    Works for me as well +1
  • Matt
    Matt over 2 years
    In vanilla js, for me this doesn't work on iOS :( the video plays for a couple seconds and pauses - it doesn't even play the whole thing to stop before a loop... any ideas?
  • cdsaenz
    cdsaenz over 2 years
    Marvellous this is it. Hot moment saved here. Had tried everything.
  • Brian Cannard
    Brian Cannard over 2 years
    playsinline worked for me. Thank you for sharing!
  • LuBre
    LuBre about 2 years
    Confirmed, it works in 2022 too. I love you.
  • addedlovely
    addedlovely about 2 years
    data-wf-ignore is not a standard attribute - what does that refer to?