用youtube api加载新视频无法正常工作

时间:2015-06-30 05:11:47

标签: javascript arrays youtube-api youtube-javascript-api

所以我试图制作一种YouTube视频请求系统。在我的电脑上,我有一个名为video.txt的文件,里面有一些youtube视频的id。 (每个人都有自己的行。)我的代码如下

<div id="player"></div>

<script src="http://www.youtube.com/player_api"></script>

<script>
    function addToList(file)
{
    var getVid = new XMLHttpRequest();
    getVid.open("GET", file, false);
    getVid.onreadystatechange = function ()
    {
        if(getVid.readyState === 4)
        {
            if(getVid.status === 200 || getVid.status == 0)
            {
                var allText = getVid.responseText;
                returnText = allText.replace( /\n/g, " " ).split( " " );
                vid.push.apply(vid, returnText);
                console.log(vid[0]);
            }
        }
    }
    getVid.send(null);
}


    //make the array
    var vid = []
    // create youtube player
    setInterval( getVid , 500 );
    var player;
    function onYouTubePlayerAPIReady() {
        player = new YT.Player('player', {
          height: '390',
          width: '640',
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
    }

    // autoplay video
    function onPlayerReady(event) {
        player.loadVideoById({'videoId': 'qjlH7pQRK9I', 'startSeconds': 0});
        event.target.playVideo();
    }

    // when video ends
    function onPlayerStateChange(event) {        
        if(event.data === 0) { 
            addToList("video.txt");
            if (vid[0] == "") {
                vid.shift();
            }
            if (vid === undefined || vid.length == 0) {
                vid.push("qjlH7pQRK9I");
                player.loadVideoById({'videoId': vid[0], 'startSeconds': 0});
                vid.shift();
                } else {
                    player.cueVideoById( vid[0]);
                    player.playVideo();
                    vid.shift();
                }


            }

        }


</script>
噢,是的。只要没有视频,它就会播放10秒钟的视频。它总是从那开始。但是在那之后,下一个视频应该弹出并开始播放......但它没有。相反,我得到了这个

http://i.stack.imgur.com/DrhJl.png

但是在控制台中没有错误...我可能正在做一些愚蠢的事......但我会喜欢一些帮助&lt; 3

1 个答案:

答案 0 :(得分:0)

setInterval在那里做什么?绝对删除它。

我也改变了文本文件的读取位置。

我认为这是个主意。

你想要的第一个视频是什么?什么都没有前10秒?

<div id="player"></div>
<script src="http://www.youtube.com/player_api"></script>
<script>
  // array containing IDs of youtube videos
  var vid = [];
  // youtube player object
  var player;

  //Loads a text file with youtube IDs, pushes them to an array ( var vid )
  function loadIDsFromFile(file) {
    var getVid = new XMLHttpRequest();
    getVid.open("GET", file, false);
    getVid.onreadystatechange = function () {
      if(getVid.readyState === 4) {
        if(getVid.status === 200 || getVid.status == 0) {
          var allText = getVid.responseText;
          returnText = allText.replace( /\n/g, " " ).split( " " );
          vid.push.apply(vid, returnText);
          console.log(vid[0]);
        }
      }
    }
    getVid.send(null);
  }
  // the Youtube API is ready to use.  Let's load the text file here; no point in waiting
  function onYouTubePlayerAPIReady() {
    loadIDsFromFile("video.txt");
    player = new YT.Player('player', {
      height: '390',
      width: '640',
      events: {
      'onReady': onPlayerReady,
      'onStateChange': onPlayerStateChange
      }
    });
  }
  // autoplay video
  function onPlayerReady(event) {
    player.loadVideoById({'videoId': 'qjlH7pQRK9I', 'startSeconds': 0});
    event.target.playVideo();
  }
  // when video ends
  function onPlayerStateChange(event) {
    if(event.data === 0) { // last video stopped playing, let's load a next one and play it
      if (vid === undefined || vid.length == 0 || ! vid[0] ) {
        vid=["qjlH7pQRK9I"];    //default, "10 seconds of nothing" video
      }
      player.cueVideoById(vid[0]);
      player.playVideo();
      vid.shift();
    }
  }
</script>