使用YouTube的使用jQuery的JavaScript API [英] Using Youtube's javascript API with jQuery
问题描述
我目前正在尝试使用YouTube API作为一个jQuery插件的一部分,我碰到了一点小问题。
I'm currently trying to use the YouTube API as part of a jQuery plugin and I've run into a bit of a problem.
在YT API的工作方式是,你加载flash播放器,当它准备将发送一个电话回叫全局函数 onYouTubePlayerReady(playerId)
。然后,您可以使用该ID以的getElementById(playerId)联合
发送JavaScript调用到Flash Player(即 player.playVideo();
)。
The way the YT api works is that you load the flash player and, when it's ready it will send a call back to a global function called onYouTubePlayerReady(playerId)
. You can then use that id combined with getElementById(playerId)
to send javascript calls into the flash player (ie, player.playVideo();
).
您可以将一个事件侦听器的播放器 player.addEventListener('onStateChange','playerState');
这将发送任何状态变化到另一个全局函数(在这种情况下, playerState
)。
You can attach an event listener to the player with player.addEventListener('onStateChange', 'playerState');
which will send any state changes to another global function (in this case playerState
).
问题是我不知道如何与一个特定的播放机的状态变化相关联。我的jQuery插件可以愉快地附加多个视频的选择,重视事件,每个人,而是一个国家实际上改变的那一刻我失去跟踪哪个球员的事情发生在
The problem is I'm not sure how to associate a state change with a specific player. My jQuery plugin can happily attach more than one video to a selector and attach events to each one, but the moment a state actually changes I lose track of which player it happened in.
我希望一些示例code可能会使事情更清晰一点。下面code,应在任何HTML文件正常工作。
I'm hoping some example code may make things a little clearer. The below code should work fine in any html file.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="application/text+html;utf-8"/>
<title>Sandbox</title>
<link type="text/css" href="http://jqueryui.com/latest/themes/base/ui.all.css" rel="stylesheet" />
<script type="text/javascript" src="http://www.google.com/jsapi"></script>
<script type="text/javascript">
google.load("jquery", "1.3.2");
google.load("jqueryui", "1.7.0");
</script>
<script type="text/javascript" src="http://swfobject.googlecode.com/svn/tags/rc3/swfobject/src/swfobject.js"></script>
<script type="text/javascript">
(function($) {
$.fn.simplified = function() {
return this.each(function(i) {
var params = { allowScriptAccess: "always" };
var atts = { id: "ytplayer"+i };
$div = $('<div />').attr('id', "containerplayer"+i);
swfobject.embedSWF("http://www.youtube.com/v/QTQfGd3G6dg&enablejsapi=1&playerapiid=ytplayer"+i,
"containerplayer"+i, "425", "356", "8", null, null, params, atts);
$(this).append($div);
});
}
})(jQuery);
function onYouTubePlayerReady(playerId) {
var player = $('#'+playerId)[0];
player.addEventListener('onStateChange', 'playerState');
}
function playerState(state) {
console.log(state);
}
$(document).ready(function() {
$('.secondary').simplified();
});
</script>
</head>
<body>
<div id="container">
<div class="secondary">
</div>
<div class="secondary">
</div>
<div class="secondary">
</div>
<div class="secondary">
</div>
</div>
</body>
</html>
您会看到outputtin的状态变化信息的console.log(),但是,就像我说的,我不知道如何告诉哪个球员它的关联。
You'll see the console.log() outputtin information on the state changes, but, like I said, I don't know how to tell which player it's associated with.
任何人有办法解决?
编辑:
对不起,我还应该提到,我已经试过包装在封闭的情况下调用。
Sorry, I should also mentioned that I have tried wrapping the event call in a closure.
function onYouTubePlayerReady(playerId) {
var player = $('#'+playerId)[0];
player.addEventListener('onStateChange', function(state) {
return playerState(state, playerId, player); } );
}
function playerState(state, playerId, player) {
console.log(state);
console.log(playerId);
}
在这种情况下, playerState
不会被调用。这是额外的沮丧。
In this situation playerState
never gets called. Which is extra frustrating.
推荐答案
编辑:
显然,调用的addEventListener
在播放
对象会导致脚本被用作这是一个XML属性的字符串传递到Flash对象 - 这排除了关闭和之类的,所以它的时间为一个老派的丑陋的黑客:
Apparently calling addEventListener
on the player
object causes the script to be used as a string in an XML property that's passed to the flash object - this rules out closures and the like, so it's time for an old-school ugly hack:
function onYouTubePlayerReady(playerId) {
var player = $('#'+playerId)[0];
player.addEventListener('onStateChange', '(function(state) { return playerState(state, "' + playerId + '"); })' );
}
function playerState(state, playerId) {
console.log(state);
console.log(playerId);
}
&测试放大器;工作!
Tested & working!
这篇关于使用YouTube的使用jQuery的JavaScript API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!