Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the reference to an existing YouTube player?

<iframe width="560" height="315" src="//www.youtube.com/embed/M7lc1UVf-VE" frameborder="0" allowfullscreen></iframe>

I have a few questions on what happens when I embed a YouTube video using source code like above. The code should generate a YouTube Player object that processes the video the way users like. When I generate a Youtube Player by myself using Youtube Player API(instead of using the embed code), I can call call functions on it.

var player; function onYouTubeIframeAPIReady() {   player = new YT.Player('player', {     height: '390',     width: '640',     videoId: 'M7lc1UVf-VE',     events: {       'onReady': onPlayerReady,       'onStateChange': onPlayerStateChange     }   }); }  //player.playVideo(); will play the video. 

My question is, how do I control the player object generated by the embed code? To put it in another way, from page http://www.youtube.com/watch?v=M7lc1UVf-VE, how do I play the video by calling SOMEPlayer.playVideo()? When you go to the url, ytplayer object is available, but it doesn't seem to contain the necessary functions.

This question might be a duplicate of this.

like image 353
Maximus S Avatar asked Oct 26 '13 06:10

Maximus S


People also ask

What is Iframe_api?

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.

What is YT player?

Youtube Player. YTPlayer helps you control all of your opened Youtube videos either using keyboard shortcuts or directly using its popup window. The player has audio controls + focus button which focus on a tab, skip button which skip the ad if any, and a button to show suggested youtube videos (exclamation mark icon).

How do you stop an iframe video?

The easy (hack) way to get a video in iframe to stop playing after you hide it, is to change the SRC property of the iframe at the same time of your hide event to something else, like a static image with your logo on it. Create a separate html file, paste the vimeo embed code there and create a vimeo player object.


2 Answers

This can be done like the following.

Given a general YouTube embed source code:

<iframe width="560" height="315" src="//www.youtube.com/embed/M7lc1UVf-VE" frameborder="0" allowfullscreen></iframe> 

a. Add a enablejsapi query param and set it to 1 in the src URL

<iframe width="560" height="315" src="//www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1" frameborder="0" allowfullscreen></iframe> 

b. Give it a unique id

<iframe id="youtube-video" width="560" height="315" src="//www.youtube.com/embed/M7lc1UVf-VE?enablejsapi=1" frameborder="0" allowfullscreen></iframe> 

c. Load YouTube iFrame API

<script src="https://www.youtube.com/iframe_api"></script> 

d. Create a player that references the existing iFrame

var player; function onYouTubeIframeAPIReady() {   player = new YT.Player('youtube-video', {     events: {       'onReady': onPlayerReady,       'onStateChange': onPlayerStateChange     }   }); }  function onPlayerReady() {   console.log("hey Im ready");   //do whatever you want here. Like, player.playVideo();  }  function onPlayerStateChange() {   console.log("my state changed"); } 
like image 196
Maximus S Avatar answered Oct 02 '22 18:10

Maximus S


var player = YT.get('id-of-youtube-iframe'); 
like image 31
user2909143 Avatar answered Oct 02 '22 18:10

user2909143