YouTube Autoplay and Full Height Background Videos

0
YouTube Autoplay and Full Height Background Videos

With the recent updates in Firefox and Chrome, autoplaying YouTube/Vimeo videos have been disabled. If you check your console (I use Firefox), you will see a warning: Feature Policy: Skipping unsupported feature name “autoplay”. Even following Google’s guidelines with adding a ?autoplay=1 to your video URL does not work. Which completely throws out YouTube videos as background videos for your website…

Unless you use the YouTube API system with a bit of javascript thrown in. Thanks to YouTube, they provided a working solution to autoplaying a youtube video. We will tweak it slightly to autoplay and loop!

Remember to change the videoId to your video ID.

With some added CSS and a non-interactive layer, we have a full with/height background player:

<!-- 0. This is the overlay div that stops users interacting with the video -->
<div style="position: fixed; z-index: -98; width: 100%; height: 100%"></div>

<!-- 1. The <iframe> (and video player) will replace this <div> tag. -->
<div id="yt-wrap">
	<div id="ytplayer"></div>
</div>

<script>
// 2. This code loads the IFrame Player API code asynchronously.
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

// 3. This function creates an <iframe> (and YouTube player)
// after the API code downloads.
var player;
function onYouTubeIframeAPIReady() {
	player = new YT.Player('ytplayer', {
		width: '100%',
		height: '100%',
		videoId: 'dQw4w9WgXcQ', // <--- Change this to your video ID
		playerVars: {
			'autoplay': 1,
			'showinfo': 0,
			'autohide': 1,
			'loop': 1,
			'controls': 0,
			'modestbranding': 1,
			'vq': 'hd1080'
		},
		events: {
			'onReady': onPlayerReady,
			'onStateChange': onPlayerStateChange
		}
	});
}

// 4. The API will call this function when the video player is ready.
function onPlayerReady(event) {
	event.target.playVideo();
	player.setVolume(0); // comment out if you don't want the auto played video muted
}

// 5. The API calls this function when the player's state changes.
// The function indicates that when playing a video (state=1),
// the player should play for six seconds and then stop.
function onPlayerStateChange(event) {
	if (event.data == YT.PlayerState.ENDED) {
		player.seekTo(0);
		player.playVideo();
	}
}
function stopVideo() {
	player.stopVideo();
}
</script>

<style>
	
</style>

The only other suggest I would add is to load the video after the page has loaded to help give the appearance of a faster loading website. Depends on your use case.

Is your website delivering SUCCESS to your business?

About The Guide
Subscribe
Notify of
guest
1 Comment
Most Voted
Newest Oldest
Inline Feedbacks
View all comments
Robert Steele

Hey thankyou for that, very much appreciated

Related Reading

07 4859 1211
© Copyright 2022 - That Website Is Me - All Rights Reserved
Privacy Policy
envelopephone-handset
1
0
Would love your thoughts, please comment.x
()
x
linkedin facebook pinterest youtube rss twitter instagram facebook-blank rss-blank linkedin-blank pinterest youtube twitter instagram