Jump to content
Search Community

playing Youtube onComplete

yammine test
Moderator Tag

Recommended Posts

Hello gsap,

 

I have an animation and i want to play a youtube video when the animation finishes.

I aded the onComplete property to play the video,

var tag = document.createElement('script');
tag.src = "https://www.youtube.com/player_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
var player1;
function onYouTubePlayerAPIReady() {
	player1 = new YT.Player('video', {
		width: '100%',
		height: '100%',
		videoId: 'reAlJKv7ptU'
	});
}
function play(){
  player1.playVideo();
}
gsap.to(".video", {duration:1 , x:100, onComplete:play})

it seems that it plays the video but it stops instantly.

 

Thank you.

See the Pen pobzVer by YammineYammine (@YammineYammine) on CodePen

Link to comment
Share on other sites

Hey yammine and welcome to the GreenSock forums.

 

It seems to work fine for me on most loads. There was one time where it did the behavior that you describe. Most likely that means that the video player is not ready to be played at the time when you told it to play. It'd be best to make sure the YouTube play is ready before trying to play the video (perhaps also before you play your animation). 

 

I haven't used the YouTube Player API so there is likely a better way to do this sort of thing, but you should be able to do something like this to make sure that the video is ready before trying to play it:

See the Pen YzWKJXq?editors=0010 by GreenSock (@GreenSock) on CodePen

Link to comment
Share on other sites

Hello ZachSaucier

This was a small demo in codepen, in my real project i actually wait for the youtube player until it is ready,

the demo that you provided still has the same issue i described.

maybe its something related to the browser or the machine that i am using.

 

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...