0

I have YouTube API which I got from this link...

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

It's working okay, but I am looking for a way for it to play a random video based on a tag...is this possible? Is there a tutorial on it?

Here is my code

<!-- 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>
user2247178
  • 21
  • 1
  • 5
  • I'm not seeing anything in this reference that indicates that you can search for a video (or a list of videos) by YouTube tag... You might [look to this question](http://stackoverflow.com/questions/5873185/youtube-api-how-to-get-videos-from-channel-x-with-tag-y) for leads in that category. – Nightfirecat Apr 05 '13 at 00:19
  • I tried adding this code `https://gdata.youtube.com/feeds/api/videos?category=bass%2Cfishing&v=2` and it did nothing :( – user2247178 Apr 05 '13 at 00:34
  • You're looking to parse the URL in the comment there and retrieve a random video entry, grab the ID and use that in the 'videoID' parameter in the onYouTubeIframeAPIReady function... aren't you? – Cal McLean Apr 05 '13 at 23:41

1 Answers1

0

Make Ajax function call to handle the request

function request_yt_videoID(vid)
{
       // Some Code
       player.loadVideoById(vid);
}

Visit Google Developer Site: https://developers.google.com/youtube/iframe_api_reference#loadVideoById

syyu
  • 409
  • 7
  • 10