2

我是 Jquery 的新手。我正在尝试使用两个按钮嵌入单个视频:播放和停止。我注意到,如果我将代码的 videoid 变量替换为真实的视频 id(例如 CkPv2jP9KeY)直接放在 html 内容上,播放和停止按钮就会起作用。到目前为止,我有这个:

<!DOCTYPE html>
<html lang="es">
<head>
<script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>   
<script type="text/javascript" src="http://www.youtube.com/player_api"></script>
<script>
var player;
function onYouTubeIframeAPIReady() {player = new YT.Player('player');}
if(window.opera){
    addEventListener('load', onYouTubeIframeAPIReady, false);
}
</script>
 </head>
<body>
<script>
$(document).ready(function(){
    $.getJSON("https://gdata.youtube.com/feeds/api/users/diasporaduo/uploads?v=2&alt=jsonc",function(json){
        videoid = json.data.items[0].id;
        $("#video").append('<iframe id="player" width="560" height="349" frameborder="0" src="http://www.youtube.com/embed/'+ videoid +'?rel=0&wmode=Opaque&enablejsapi=1"></iframe>');
    });
 });
</script>
<div id="video"></div>
<a href="javascript: void(0)" onclick="player.playVideo(); return false">play</a><br>
<a href="javascript: void(0)" onclick="player.stopVideo(); return false">stop</a>
</body>
</html>
4

2 回答 2

1

您可以像这样重新排列您的代码:

  1. 包括https://www.youtube.com/player_api
  2. 创建一个空的 div 元素,最好与视频大小相同
  3. 实现执行onYouTubeIframeAPIReady以下操作:
    • 创建一个空白的 YouTube iframe 播放器
    • onReady在事件中加载视频

笔记:

  • 从 GData 中检索 JSON 数据不需要 jQuery。您可以简单地创建一个<script>with src = https://gdata...&alt=jsonc&callback=functionThatCallsCueVideo
  • 可以重新排列代码。例如,您可以先加载 JSON,然后调用player = new YT.Player({videoId: 'u1zgFlCw8Aw'})

HTML:

<!-- this could could inside head -->
<script type="text/javascript" src="https://code.jquery.com/jquery-latest.min.js"></script>
<script type="text/javascript" src="https://www.youtube.com/player_api"></script>

<!-- this goes in body -->
<div id="video"></div>
<a href="javascript: void(0)" onclick="player.playVideo(); return false">play</a><br>
<a href="javascript: void(0)" onclick="player.stopVideo(); return false">stop</a>

JavaScript:

var player;
function onYouTubeIframeAPIReady() {
    player = new YT.Player('video', {
        width: '560',
        height: '349',
        events: {
            'onReady': function () {
                $.getJSON("https://gdata.youtube.com/feeds/api/users/diasporaduo/uploads?v=2&alt=jsonc", function (json) {
                    videoid = json.data.items[0].id;
                    player.cueVideoById(videoid);
                });
            }
        }
    });
}

演示

于 2013-01-21T21:11:03.103 回答
0

感谢 Salman A。这是他回复的 Jquery 工作版本:

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="utf-8" />
    <title>Title</title>
     <!--[if lt IE 9]>
    <script src="http://html5shim.googlecode.com/svn/trunk/html5.js"></script>
    <![endif]-->
    <link href="css5/reset.css" rel="stylesheet" />
    <link href="css5/main.css" rel="stylesheet" />

     <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>   
     <script type="text/javascript" src="http://www.youtube.com/player_api"></script>

</head>
<body>
<script>
var player;

$(document).ready(function(){
    window.onYouTubeIframeAPIReady = function() {
        player = new YT.Player('video', {
          events: {
            'onReady': function () {
                $.getJSON("http://gdata.youtube.com/feeds/api/users/diasporaduo/uploads?v=2&alt=jsonc", function (json) {
                    videoid = json.data.items[0].id;
                    player.cueVideoById(videoid);
                });
            }
        }
    });
}
});

</script>

<div id="video"></div>


<a href="javascript: void(0)" onclick="player.playVideo(); return false">play</a><br>
<a href="javascript: void(0)" onclick="player.stopVideo(); return false">stop</a>


</body>
</html>
于 2013-01-22T14:18:49.867 回答