2013-04-05 60 views
0

随机播放视频我的YouTube API,我从这个链接了......的YouTube API从标签

https://developers.google.com/youtube/iframe_api_reference

它的工作好,但我在寻找一种方式,它起到一个随机基于标签的视频......这可能吗?有关于它的教程吗?

这里是我的代码

<!-- 1. The <iframe> (and video player) will replace this <div> tag. --> 
    <div id="player"></div> 

    <script> 
     // 2. This code loads the IFrame Player API code asynchronously. 
     var tag = document.createElement('script'); 

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

     // 3. This function creates an <iframe> (and YouTube player) 
     // after the API code downloads. 
     var player; 
     function onYouTubeIframeAPIReady() { 
     player = new YT.Player('player', { 
      height: '390', 
      width: '640', 
      videoId: 'u1zgFlCw8Aw', 
      events: { 
      'onReady': onPlayerReady, 
      'onStateChange': onPlayerStateChange 
      } 
     }); 
     } 

     // 4. The API will call this function when the video player is ready. 
     function onPlayerReady(event) { 
     event.target.playVideo(); 
     } 

     // 5. The API calls this function when the player's state changes. 
     // The function indicates that when playing a video (state=1), 
     // the player should play for six seconds and then stop. 
     var done = false; 
     function onPlayerStateChange(event) { 
     if (event.data == YT.PlayerState.PLAYING && !done) { 
      setTimeout(stopVideo, 6000); 
      done = true; 
     } 
     } 
     function stopVideo() { 
     player.stopVideo(); 
     } 
    </script> 
+0

我不是在此引用,表示看到任何东西你可以通过YouTube标签搜索视频(或视频列表)...你可能会[看这个问题](http://stackoverflow.com/questions/5873185/youtube-api-how-to-get -vide os-from-channel-x-with-tag-y)用于该类别的潜在客户。 – Nightfirecat 2013-04-05 00:19:28

+0

我试着添加这个代码:https://gdata.youtube.com/feeds/api/videos?category = bass%2Cfishing&v = 2',它什么都没做: – user2247178 2013-04-05 00:34:30

+0

您正在寻找解析注释中的URL并检索一个随机视频条目,获取ID并在onYouTubeIframeAPIReady函数的'videoID'参数中使用...不是吗? – 2013-04-05 23:41:14

回答