這是我。超過1年後,當我看到這個問題沒有被任何人回答。 我決定寫下我的解決方案,希望這會有所幫助。
- 導入谷歌YouTube播放器
<script src="http://www.google.com/jsapi" type="text/javascript"></script>
添加js代碼
google.load("swfobject", "2.1");
function updateHTML(elmId, value) {
document.getElementById(elmId).innerHTML = value;
}
// Loads the selected video into the player.
function loadVideo() {
var selectBox = document.getElementById("videoSelection");
var videoID = selectBox.options[selectBox.selectedIndex].value
if (ytplayer) {
ytplayer.cueVideoById(videoID);
}
}
// This function is called when an error is thrown by the player
function onPlayerError(errorCode) {
alert("An error occured of type:" + errorCode);
}
var target = "";
// This function is automatically called by the player once it loads
function onYouTubePlayerReady(playerId) {
ytplayer = document.getElementById("ytPlayer");
ytplayer.addEventListener("onError", "onPlayerError");
ytplayer.addEventListener("onStateChange","onPlayStateChange");
target = document.forms[0].target;
}
//unstarted (-1), ended (0), playing (1), paused (2), buffering (3), video cued (5)
var n = 0;
function onPlayStateChange(newState) {
if (newState == 1) {
document.forms[0].elements["Send"].value = "1";
document.forms[0].target = "SendFrame";
document.forms[0].submit();
updateHTML("counter", ++n);
}
else {
document.forms[0].elements["Send"].value = "0";
document.forms[0].target = target;
}
}
// The "main method" . Called when someone clicks "Run".
function loadPlayer() {
// The video to load
var videoID = "ylLzyHk54Z0"
// Lets Flash from another domain call JavaScript
var params = { allowScriptAccess: "always" };
// The element id of the Flash embed
var atts = { id: "ytPlayer" };
// All of the magic handled by SWFObject (http://code.google.com/p/swfobject/)
swfobject.embedSWF("http://www.youtube.com/v/" + videoID +
"&enablejsapi=1&playerapiid=player1",
"videoDiv", "480", "295", "8", null, null, params, atts);
}
function _run() {
loadPlayer();
}
google.setOnLoadCallback(_run);
</script>
添加HTML代碼2個div元素,一個是球員,一個計數器
<div id="videoDiv">Load...</div>"
<div id="counter">0</div>
好了,這就是你需要做的:)
我已經inplimented使用JavaScript類似的東西Youtube API。 [鏈接](http://www.manishojha.com)..讓我知道如果你需要別的什麼... – manish 2011-02-16 07:47:47