2012-11-01 14 views
20

गतिशील रूप से कैसे बदलें I YT.Player कोड का उपयोग करना चाहते हैं ताकि मैं ईवेंट का पता लगा सकूं। मेरे पास यूट्यूब वीडियो आईडी और एक ऐसा फ़ंक्शन है जो आईडी को सरणी में रखता है और जब कोई उपयोगकर्ता किसी छवि पर क्लिक करता है तो गतिशील रूप से आईडी को बदलने में सक्षम होना चाहता है।यूट्यूब प्लेयर वीडियोआईडी

// 2. This code loads the IFrame Player API code asynchronously. 
var tag = document.createElement('script'); 
tag.src = "//www.youtube.com/iframe_api"; 
var firstScriptTag = document.getElementsByTagName('script')[0]; 
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag); 

// 3. This function creates an <iframe> (and YouTube player) 
// after the API code downloads. 
// NOTE: videoId is taken out and instead is placed as the generated IFRAME src from the postSelection function 

var player; 
function onYouTubeIframeAPIReady() { 
    player = new YT.Player('player', { 
     height: '100%', 
     width: '100%', 
     videoId: '<<CALL TO FUNCTION OR VARIABLE HERE>>', 
     events: { 
     //'onReady': onPlayerReady, 
     'onStateChange': onPlayerStateChange 
     } 
    }); 
} 

एचटीएमएल

<!-- 1. The <iframe> (and video player) will replace this <div> tag. --> 
<div id="player"></div> 

जावास्क्रिप्ट आप इस कोड को तो क्या होता है #player DIV एक IFRAME की जगह से परिचित हैं: मूलतः यह पसंद है। मैं इस फ़ंक्शन के साथ IFRAME स्रोत को बदल सकता हूं:

function postSelection() { 
    $("#player").attr("src", _selected.attributes.url); //_selected.attributes.url comes from the CVS file 
} 

लेकिन यह बहुत छोटी है और ब्राउज़र अनुकूल नहीं है। यदि मैं एक मानक IFRAME का उपयोग करता हूं और वाईटी प्लेयर एपीआई नहीं तो सब कुछ ठीक काम करता है। लेकिन मैं अंत का पता लगाना चाहता हूं, और रोकें और इसलिए मुझे एपीआई का उपयोग करना होगा। मेरा अनुमान है कि यह राज्य के साथ एक मुद्दा है और आईएफआरएएम के निर्माण में कुछ दृढ़ता खो गई है।

सम्मान।

+1

क्या आपने [यूट्यूब एपीआई के दस्तावेज़ीकरण] (https://developers.google.com/youtube/iframe_api_reference) को भी पढ़ा था? आप एपीआई कॉल करने के लिए अपने YouTubeYouframeAPIReady में प्राप्त प्लेयर वैरिएबल का उपयोग कर सकते हैं। – Philipp

उत्तर

43

यह जेएस एपीआई के साथ करना बहुत आसान है। जब आप एक नया वीडियो लोड करना चाहते हैं तो player.loadVideoById(videoId); पर कॉल करें https://developers.google.com/youtube/iframe_api_reference#loadVideoById

+3

ध्यान दें कि यह आईफ्रेम एपीआई के साथ है, क्योंकि "जावास्क्रिप्ट एपीआई" अब बहिष्कृत है। – sheriffderek