How should I use Youtube API?

How does Quora use the Youtube Javascript API to pause a playing video when the user plays another video on the same page?

  • Answer:

    I built the video feature on Quora. It's pretty easy to pause the playing YouTube video when the user clicks on another video. You just need to catch the click event and tell all YouTube players on the page to pause themselves before starting the video the user actually clicked on. You can do it with something like this: // Store a reference to each YouTube player DOM element in an array. // Here I'll just assume two players and hard-code them. var ytplayer1 = document.getElementById("playerId1"); var ytplayer2 = document.getElementById("playerId2"); var players = [ytplayer1, ytplayer2]; // Assuming that all YouTube players on the page have the class "player" $(".player").click(function(event) { // Pause all videos for(var i = 0; i < players.length; i++) { players[i].pauseVideo(); } // Play the clicked video $(event.target).playVideo(); });

Feross Aboukhadijeh at Quora Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.