YouTube IFrame API 播放/停止不工作



我是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>

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

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

笔记:

  • 您不需要 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);
                });
            }
        }
    });
}

演示

感谢萨尔曼·这是他的回应的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>

相关内容

  • 没有找到相关文章

最新更新