HTML5 Video autoplay on iPhone
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:
- Check video is playing or not.
- 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
Solution 5
I had a similar problem and I tried multiple solution. I solved it implementing 2 considerations.
- 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>`}}
/>
- 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+)
Related videos on Youtube
SeBa
Updated on December 24, 2021Comments
-
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
-
Mark about 7 yearsYou might be able to find some help here: stackoverflow.com/questions/41360490/… Personally, I have yet to get any video at all to autoplay on an iphone, even after following all those tips and Apples policies.
-
BoltCoder almost 4 yearsIt 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. medium.com/@BoltAssaults/…
-
chickens over 2 yearsDuplicate: stackoverflow.com/questions/39259418/…
-
Mathieu almost 6 yearsgreat tip but i have a question about this that I submitted : stackoverflow.com/questions/50400902/…
-
tolmark almost 6 yearsHalleluja! Thank you for this, had tried about a dozen other solutions to no avail.
-
Ken almost 6 years
playsinline
worked for me in conjunction withmuted
keeping in mind the low power mode quirk on iPhone -
JCraine over 5 yearsThis is the answer.
-
lior over 5 yearsI spent the last hour or so trying to understand why my videos were not playing automatically. Thank you for this!
-
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 over 5 yearsThank you for this!
-
Muhammad Osama over 5 yearsIts 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 over 5 yearsI'm not joking, you saved my life
-
Quentin D over 5 yearsIf you're using React, note that the attribute
playsinline
must be written in camelCase:playsInline
. Otherwise it won't work. -
haeki about 5 yearsHappened 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 over 4 yearsDon't forget to add muted also with playsinline. playsinline attribute won't work alone without muted attribute
-
Zankar over 4 yearsThis works - thank you! Just another iOS exception to worry about :D
-
Hugo Nogueira over 4 years@QuentinD you saved my life. Now working in all devices including iOS.
-
B-Money almost 3 yearsYes 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 almost 3 yearsWorks for me as well +1
-
Matt over 2 yearsIn 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 over 2 yearsMarvellous this is it. Hot moment saved here. Had tried everything.
-
Brian Cannard over 2 yearsplaysinline worked for me. Thank you for sharing!
-
LuBre about 2 yearsConfirmed, it works in 2022 too. I love you.
-
addedlovely about 2 yearsdata-wf-ignore is not a standard attribute - what does that refer to?