Video auto play is not working in Safari and Chrome desktop browser
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.
Solution 1:
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:
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
andmuted
are set[...]<video autoplay muted> <source src="video.webm" type="video/webm" /> <source src="video.mp4" type="video/mp4" /> </video>
- 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:
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:
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:
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>`