I am struggling with setting up the loop for youtube videos using youtube player api.
The problem is that the video is not running under a loop.
Here is my code
var tag = document.createElement('script');
tag.src = "https://www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player;
function onYouTubeIframeAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
playerVars: {
'controls': 0,
'showinfo': 0,
'rel': 0,
'loop': 1
},
videoId: 'qzZuBWMnS08',
events: {
'onReady': onPlayerReady,
// 'onStateChange': onPlayerStateChange
}
});
}
function onPlayerReady(event) {
// event.target.setLoop(true);
event.target.playVideo();
}
The loop:1 doesn't seem to be working.Also is there any way to remove the share and video title from the top of the video.
Thanks in advance.
Developers can use the iFrame API to programmatically create and interact with an Embed or with multiple Embeds in the same web app. The iFrame API includes methods that you can use to start playback, change the content rendering in an Embed, or stop playback.
The IFrame player API lets you embed a YouTube video player on your website and control the player using JavaScript. Using the API's JavaScript functions, you can queue videos for playback; play, pause, or stop those videos; adjust the player volume; or retrieve information about the video being played.
If the video isn't changing, you can just do
onStateChange:
function(e) {
if (e.data === YT.PlayerState.ENDED) {
player.playVideo();
}
}
This will prevent unnecisarily reloading the video
This is what I have used for the API IFrame video loop. I noticed that you must include "playlist:VIDEO_ID" parameter. and it works. This is my example
<script>
// Load 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);
// Replace the 'ytplayer' element with an <iframe> and
// YouTube player after the API code downloads.
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('ytplayer', {
height: '390',
width: '640',
videoId: 'VIDEO_ID',
playerVars: {
playlist: 'VIDEO_ID',
loop: 1
}
});
}
</script>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With