gpt4 book ai didi

javascript - 处理多个 youtube 视频

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:04:20 25 4
gpt4 key购买 nike

我有三个 youtube 视频,当用户点击链接页面时,我想停止这些视频。这是我的代码

var tag = document.createElement('script');
tag.src = "//www.youtube.com/iframe_api";
var firstScriptTag = document.getElementsByTagName('script')[0];
firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

var youtubePlayer1;
var youtubePlayer2;
var youtubePlayer3;
function onYouTubeIframeAPIReady() {
youtubePlayer1 = new YT.Player('firstPlayer', {

});
youtubePlayer2 = new YT.Player('secondPlayer', {

});
youtubePlayer3 = new YT.Player('thirdPlayer', {

});
}

function stopVideo() {
if (youtubePlayer1 != null) {
youtubePlayer1.stopVideo();
}
if (youtubePlayer2 != null) {
youtubePlayer2.stopVideo();
}
if (youtubePlayer3 != null) {
youtubePlayer3.stopVideo();
}
}

这是html代码

<div id="blog">
<!--///////////// UN ORDERED LIST /////////////-->
<ul>
<!--///////////// LIST /////////////-->
<li>
<!-- iframe -->
<h3>
<strong>להקת קולות - בהרקדה חסידית...</strong></h3>
<br />
<iframe id="firstPlayer" width="800" height="485" src="http://www.youtube.com/embed/F0eR1KFkt58"
style="border:0" ></iframe>
<br />
<br />
<img src="images/bg3.PNG" alt="" /><p>
<span>תאור הוידאו: </span>טקסט אודות הוידאו, תאריך</p>
</li>
<!--///////////// SECOND IMAGE /////////////-->
<li>
<!-- iframe -->
<h3>
<strong>להקת קולות - בהרקדה ישראלית מוטרפת...</strong></h3>
<br />
<iframe id="secondPlayer" width="800" height="485" src="http://www.youtube.com/embed/mPTX4guU1W8"
style="border:0" ></iframe>
<br />
<br />
<img src="images/bg3.PNG" alt="" /><p>
<span>תאור הוידאו: </span>טקסט אודות הוידאו, תאריך</p>
</li>
<!--///////////// THIRD IMAGE /////////////-->
<li>
<!-- iframe -->
<h3>
<strong>להקת קולות - בואי בשלום...</strong></h3>
<br />
<iframe id="thirdPlayer" width="800" height="485" src="http://www.youtube.com/embed/E-_ONZOcScU"
style="border:0"></iframe>
<br />
<br />
<img src="images/bg3.PNG" alt="" /><p>
<span>תאור הוידאו: </span>טקסט אודות הוידאו, תאריך</p>
</li>
</ul>
</div>

当用户单击链接时,它会调用 stopVideo 函数,该函数循环遍历所有播放器并停止播放。

出于某种原因,我只能让它在 youtubePlayer2 对象上工作,我在这里做错了什么?

忘记提及,当我使用 chrome 调试器调试应用程序时,我可以看到对象已定义并且函数已被调用。

最佳答案

我认为这种方式可行:

http://jsfiddle.net/ZLMF8/3/

<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript"

src="https://ajax.googleapis.com/ajax/libs/swfobject/2/swfobject.js"></script></head>

<body>

<div id="ytplayer1">
<p>You will need Flash 8 or better to view this content.</p>

</div>

<div id="ytplayer2">
<p>You will need Flash 8 or better to view this content.</p>

</div>

<div id="ytplayer3">
<p>You will need Flash 8 or better to view this content.</p>

</div>

<script type="text/javascript">

var params = { allowScriptAccess: "always" };

swfobject.embedSWF("http://www.youtube.com/v/F0eR1KFkt58&enablejsapi=1&playerapiid=ytplayer1", "ytplayer1", "425", "365", "8", null, null, params);

swfobject.embedSWF("http://www.youtube.com/v/mPTX4guU1W8&enablejsapi=1&playerapiid=ytplayer2", "ytplayer2", "425", "365", "8", null, null, params);

swfobject.embedSWF("http://www.youtube.com/v/E-_ONZOcScU&enablejsapi=1&playerapiid=ytplayer3", "ytplayer3", "425", "365", "8", null, null, params);

function stop() {

if (ytplayer1) {

ytplayer1.stopVideo();

}
if (ytplayer2) {

ytplayer2.stopVideo();

}
if (ytplayer3) {

ytplayer3.stopVideo();

}

}​

</script>

<a href="javascript:void(0);" onclick="stop();">Stop</a>


</body>

</html>​

关于javascript - 处理多个 youtube 视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13043799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com