I need to make a video page for my website which must contain a video from youtube (https://www.youtube.com/watch?v=bWPMSSsVdPk) and after this video ended I need to automatically display a second video (https://www.youtube.com/watch?v=0afZj1G0BIE) and the div
with class content
This is my code so far:
<style>
.content {
display: none;
}
</style>
<div class="content">
<button>Show this button after first video ends</button>
</div>
<div id="player"></div>
<script src="http://www.youtube.com/player_api"></script>
<script>
// create youtube player
var player;
function onYouTubePlayerAPIReady() {
player = new YT.Player('player', {
height: '390',
width: '640',
videoId: 'bWPMSSsVdPk',
events: {
'onReady': onPlayerReady,
'onStateChange': onPlayerStateChange
}
});
}
// autoplay video
function onPlayerReady(event) {
event.target.playVideo();
}
// when video ends
function onPlayerStateChange(event) {
if(event.data === 0) {
alert('done');
}
}
</script>
I've found a solution for this but it seems that the sound of the second video is also playing in the same time with the first video...
That'll play your next video right after your first. However that also calls playVideo() which will be an issue for anyone watching on an Apple mobile device. Consider using cueVideoById() instead.
There is no direct way as to how one can do this with the available set of parameters that a Youtube iframe Player API has. But you can try doing this.
Retrieve the playlistID of that particular playlist.
Finally, set the autoplay parameter to 1 so that the video's in the playlist play automatically.