2012-08-15 176 views
1

所以基本上,我有一个弹出的代码,使分区弹出。里面有一个YouTube视频播放器。一切正常,直到我关上窗户,我仍然可以在背景中听到音乐,这意味着视频仍在播放。我想阻止它。 我做了一些研究,并落在YouTube的API上,但它仍然无法正常工作。弹出窗口中的Youtube视频不会停止播放当窗口关闭

这里是我的股利

echo "<div id='popupContact'>"; 
echo "<a id='popupContactClose'>x</a>"; 
echo "<div><h1>" .$row['title']. "</h1></div>"; 
echo "<p id='contactArea'>"; 
if($row[type] == "image") 
echo "<img src='gallery/" .$row['path']. "' alt='" .$row['title']. "'/>"; 
else 
echo "<iframe width='560' height='315' src='http://www.youtube.com/v/" .$row['path']."?  enablejsapi=1&version=3&playerapiid=ytplayer' frameborder='0' allowfullscreen></iframe>"; 
echo "<br/><br/>" .$row['description']. "</p>"; 
echo "</div>"; 
echo "<div id='backgroundPopup'></div>"; 

代码和这里是弹出代码:

//CONTROLLING EVENTS IN jQuery 
$(document).ready(function(){ 

//LOADING POPUP 
//Click the button event! 
$("#button").click(function(){ 
    //centering with css 
    centerPopup(); 
    //load popup 
    loadPopup(); 
}); 

//CLOSING POPUP 
//Click the x event! 
$("#popupContactClose").click(function(){ 
    disablePopup(); 
}); 
//Click out event! 
$("#backgroundPopup").click(function(){ 
    disablePopup(); 
}); 
//Press Escape event! 
$(document).keypress(function(e){ 
    if(e.keyCode==27 && popupStatus==1){ 
     disablePopup(); 
    } 
}); 

}); 

//disabling popup with jQuery magic! 
function disablePopup(){ 
//disables popup only if it is enabled 
if(popupStatus==1){ 
    $("#backgroundPopup").fadeOut("slow"); 
$("#popupContact").fadeOut("slow"); 
popupStatus = 0; 
stop(); 
} 
} 
function onYouTubePlayerReady(playerId) { 
alert("YAY"); 
ytplayer = document.getElementById("ytplayer"); 
} 

function stop() { 
if (ytplayer) { 
ytplayer.stopVideo(); 
} 
} 

谢谢 阿糖胞苷

+0

我没有看到代码的函数'loadPopup()'当点击#键时调用.. – jmm 2012-08-15 21:22:14

+0

功能loadPopup(){ \t //只加载弹出,如果它被禁用 \t如果( 。popupStatus == 0){ \t \t $( “#backgroundPopup”)的CSS({ \t \t \t “不透明性”: “0.7” \t \t}); \t \t $(“#backgroundPopup”)。fadeIn(“slow”); (“#popupContact”)。fadeIn(“slow”); \t \t popupStatus = 1; \t} } – user1049769 2012-08-16 00:33:32

+0

您是否查看了我在以下答案中列出的文档? – jmm 2012-08-16 01:42:15

回答

0

我一直在寻找在Youtube Documentation(iframe),我认为你是混合了不同的API

JavaScript API它规定

我们推荐使用SWFObject来嵌入任何将使用JavaScript API访问的玩家。

但是您使用的是iframe。所以,我认为你需要创建一个函数调用

function onYouTubeIframeAPIReady()

其中有几个行代码设置都与其他功能一起。

0

变量范围。您只能在onYouTubePlayerReady()函数中定义ytplayer,并且不返回该值。当该函数返回时,ytplayer值被销毁。

您需要定义函数的变量外:

var ytplayer; // now a global variable. 
function onYouTubePlayerReadye(playerID) { 
    ytplayer = document.getElementById('ytplayer'); 
} 
+0

虽然没有工作....我认为onYouTubePlayerReady(playerId)永远不会被调用。我在函数中放置了测试警报,它永远不会被调用 – user1049769 2012-08-15 19:19:14