从我的频道中提取最后3个YouTube视频

时间:2015-04-02 12:22:30

标签: javascript youtube youtube-api

我目前能够从我的频道中提取最新的YouTube视频,但我想将其更改为显示最后3个视频,但我似乎可以让它发挥作用。

我目前正在使用此JavaScript:

 <script type="text/javascript">
    function showVideo(response) {
        if(response.data && response.data.items) {
            var items = response.data.items;
            if(items.length>0) {
                var item = items[0];
                var videoid = "http://www.youtube.com/embed/"+item.id;
                console.log("Latest ID: '"+videoid+"'");
                var video = "<iframe width='420' height='315' class='posvid' src='"+videoid+"' frameborder='0' allowfullscreen></iframe>"; 
                $('#static_video').html(video);
            }
        }
    }
</script>
<script type="text/javascript" src="https://gdata.youtube.com/feeds/api/users/gezzamondoTV/uploads?max-results=1&orderby=published&v=2&alt=jsonc&callback=showVideo"></script>

这是最新版本,是否有人知道要添加或更改内容以获取最近3个最近的视频?

1 个答案:

答案 0 :(得分:3)

首先将URL中的max-results = 1参数设置为3。

然后写下这样的javascript:

<script type="text/javascript">
    function showVideo(response) {
        if(response.data && response.data.items) {
            var items = response.data.items;
            for(var i=0;i<items.length;i++) {
                var item = items[i];
                var videoid = "http://www.youtube.com/embed/"+item.id;
                console.log("Latest ID: '"+videoid+"'");
                var video = "<iframe width='420' height='315' class='posvid' src='"+videoid+"' frameborder='0' allowfullscreen></iframe>"; 
                $('#static_video'+i).html(video);
            }
        }
    }
</script>
<script type="text/javascript" src="https://gdata.youtube.com/feeds/api/users/gezzamondoTV/uploads?max-results=3&orderby=published&v=2&alt=jsonc&callback=showVideo"></script>

然后在html中显示三个这样的视频:

<div id="static_video0"><h2>Video 1</h2></div>
<div id="static_video1"><h2>Video 2</h2></div>
<div id="static_video2"><h2>Video 3</h2></div>