YouTube IFrame API播放/停止无效

时间:2013-01-21 20:45:06

标签: jquery html5 youtube-api

我是Jquery的新手。我试图用两个按钮嵌入一个视频:播放和停止。我注意到如果我将带有videoid变量的代码直接替换为html内容上的真实视频ID(例如CkPv2jP9KeY),则播放和停止按钮可以正常工作。到目前为止,我有这个:

<!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>

2 个答案:

答案 0 :(得分:1)

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

  1. 包括https://www.youtube.com/player_api
  2. 创建一个空div元素,最好与视频
  3. 相同
  4. 实施onYouTubeIframeAPIReady执行以下操作:
    • 创建一个空白的YouTube iframe播放器
    • onReady活动
    • 中加载视频
  5. 注意:

    • 您不需要jQuery来从GData检索JSON数据。您只需使用src =创建<script>即可 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);
                    });
                }
            }
        });
    }
    

    Demo

答案 1 :(得分: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>