Cross browser video autoplay loop - javascript

I have a copy of a mp4 video which I want to show on my website and I want to know how is it possible with javascript to autoplay and loop for ever the video.
I want to make it compatible with all browsers.
I want to load the video on load of the page with js
Thank you.

You’ll need to mute the video (adding the muted attribute on the <video> tag) as videos with sound don’t autoplay in all browsers. Then also add the attributes autoplay and playsinline. Also you can use the videoEnd event and call play() to ensure it loops.

<video autoplay loop>
<source src="movie.mp4" type="video/mp4" />
<source src="movie.ogg" type="video/ogg" />
</video>
You mean this?

Related

Replace background video with image if autoplay is not supported in vue js

I have a website with a background video. Many browsers especially mobile ones dont support autoplay even with muted videos. How can i detect if the browser isnt supporting autoplay and replace the video with an image. Im using vueJS.
<video autoplay class="background-video" loop muted> <source src="#/assets/video/background.mp4" type="video/mp4"> </video>
Use the poster attribute:
<video width="620" loop muted poster="https://upload.wikimedia.org/wikipedia/commons/e/e8/Elephants_Dream_s5_both.jpg" >
<source
src="https://archive.org/download/ElephantsDream/ed_1024_512kb.mp4"
type="video/mp4">
<source
src="https://archive.org/download/ElephantsDream/ed_hd.ogv"
type="video/ogg">
<source
src="https://archive.org/download/ElephantsDream/ed_hd.avi"
type="video/avi">
</video>
source
Example with no video
Add the playsinline attribute for your video to autoplay on iOS.
(Most browsers support autoplay, as long as you also use muted.)

How to make MP3 file play in the background of HTML/CSS/JS page?

Would like to have an MP3 track play in the background immediately after a HTML/CSS/JavaScript page is loaded.
So far, only able to have a player with controls at the bottom of the page:
Bottom of webpage
using the following code:
<audio id="bg_music" class="audio" controls loop>
<source src="audio/silent-night.mp3"
type="audio/mp3">
Your browser does not support the audio element.
</audio>
Would like to find a way to have the MP3 file play automatically after the browser (Chrome) loads the webpage.
Thanks,
Benjamin
You need to add an attributed autoplay, and it will be done. You may additionaly provide value autoplay=true or autoplay=false, but still if you don't give it, it will automatically consider it to be true if you just add the attribute.
<audio id="bg_music" class="audio" autoplay controls loop>
<source src="https://www.soundhelix.com/examples/mp3/SoundHelix-Song-1.mp3" type="audio/mp3" />
Your browser does not support the audio element.
</audio>

Autoplay video on mobile device

I've seen posts complaining that you cannot autoplay a video on mobile device.
But, this CAN be done. I've visited youtube website on android and iOS and video does indeed start to play without any user interaction. How can I mimic this behaviour?
EDIT: if you visit direct link, it does not work. If you first visit youtube.com and than click on some video, it works. No idea why.
<video id="mobilevideo" name="Mobile video" onLoad="play()" onClick="play()" preload="auto" autoplay preload muted playsinline webkit-playsinline>
<source src="file.webm" type="video/webm">
<source src="file.ogg" type="video/ogg">
<source src="file.mp4" type="video/mp4">
Your browser does not support this file type.
</video>
<!--Javascript autoplay video -->
<script>
document.getElementById('mobilevideo').play();
</script>
You can use autoplay in video tag. like this:
<video width="320" height="240" controls autoplay>
For more information, you can learn from here:
https://www.w3schools.com/tags/tryit.asp?filename=tryhtml5_video_autoplay
For mobile, you should not do auto play because it will cost user's bandwidth(money). See this post for more detail:
Can you autoplay HTML5 videos on the iPad?

How to pause/mute S3 video which is embedded in an iframe?

We are hosting a careersites for customers, one of our customers provided the data which contains video from S3 which is embedded in an iframe.
Problem is the video is getting played automatically when page is loaded. Customer is asking to pause or mute the video on page load.
Is there any way we can pause the video?
Unless there is a special reason you need to use an iframe, the usual way to handle video now is with the HTML5 video tag.
This has an attribute 'autoplay' which if present will start the video automatically, and if absent will not start the video:
http://www.w3schools.com/tags/att_video_autoplay.asp
The following will autoplay (if the devices supports this - some mobile device do not to save on data charges):
<video controls autoplay>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>
and this will not:
<video controls>
<source src="movie.mp4" type="video/mp4">
<source src="movie.ogg" type="video/ogg">
Your browser does not support the video tag.
</video>

Have HTML5 video reset itself to beginning after it plays through?

Is there a way to have a HTML5 video reset itself to the beginning after it plays through? Will be displaying the video through Safari on an iPad primarily.
Just add the loop attribute in your <video> tag.
<video loop>
<source src="something.mp4" type="video/mp4">
</video>

Categories