使用 YouTube API 将 YouTube 播放列表中的所有视频 ID 获取到 PHP 数组中



我正在向我的网站添加一个自定义播放器,但我有两个问题。

  1. 我在YouTube上的播放列表有391首歌曲,API播放器只加载200首
  2. 我在使用API命令打乱列表时遇到问题

这是我用来调用玩家的代码:

<div class="videoplayer" id="ytplayer"></div>
<script>
    var getRandom = function(min, max) {
      return Math.random() * (max - min) + min;
    };
  // Load the IFrame Player API code asynchronously.
  var tag = document.createElement('script');
  tag.src = "https://www.youtube.com/player_api";
  var firstScriptTag = document.getElementsByTagName('script')[0];
  firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  // Replace the 'ytplayer' element with an <iframe> and
  // YouTube player after the API code downloads.
  var player;
  function onYouTubePlayerAPIReady() {
    player = new YT.Player('ytplayer', {
      height: '50',
      width: '400',
      events: {
         'onReady': onPlayerReady
       },
      playerVars:{        
        list:'PLH30k6CwlcgK8C-LET6_ZrAWwYGLqT8R-',
        index:parseInt(0),
        suggestedQuality:'small',
        controls:0,
        autoplay:1
        }
    });
  }
  function onPlayerReady(event) {
     event.target.setShuffle();
     event.target.setLoop();     
   }
</script>

我试着用来解决洗牌的问题

function onPlayerReady(event) {
         var num = parseInt(getRandom(1,391));
         event.target.playVideoAt(num);   
       }

但它并不像我想要的那样工作,所以我想将播放列表中所有视频的ID检索到一个数组中,混合它们,然后传递所有数组来加载每个视频。我试图使用之前问题中的一些例子,但这些例子已经不起作用了,如果你们中的一些人有YouTube API v3的例子或实际起作用的例子,我会很感激的

谨致问候。

首先,您必须使用iframe API因为,javascript API已经过时并且不推荐使用

查找youtube API文档(左侧菜单中不推荐使用的API列表)https://developers.google.com/youtube/iframe_api_reference?hl=fr

更换

tag.src = "https://www.youtube.com/player_api";
// by 
tag.src = "https://www.youtube.com/iframe_api";

function onYouTubePlayerAPIReady()
// by 
function onYouTubeIframeAPIReady()

0若要修复混洗,请尝试在每个结束状态随机化

通过使用onPlayerStateChange事件侦听器和YT.PlayerState.ENDED状态

1你需要CUE播放列表才能获得整个视频的ID

在随机化之前。并使用适当的方法获取它们:getPlaylist()

var player;
var videoList, videoCount;
function onYouTubeIframeAPIReady()
{
    player = new YT.Player('ytplayer',
    {
        height: '50',
        width: '400',
        events:
        {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
        },
        playerVars:
        {
            controls:0,
            autoplay:0
        }
    });
}
function onPlayerReady(event)
{
    // cue the playlist, to get the video's ids 
    event.target.cuePlaylist
    ({
        listType: 'playlist',
        list: 'PLH30k6CwlcgK8C-LET6_ZrAWwYGLqT8R-',
        suggestedQuality:'small',
        autoplay: 1,
        index:0,
    });
    event.target.setLoop();
}
function onPlayerStateChange(event)
{
    if(event.data == YT.PlayerState.CUED)
    {
        videoList = event.target.getPlaylist();
        // to prevent adding new video and for the randomize
        videoCount = videoList.length; 
        // get the ids before randomize playlist, send it
        sendIds(videoList);
        // starting the player (like autoplay)
        event.target.playVideo();
    }
    // randomize at each video ending
    if(event.data == YT.PlayerState.ENDED)
    {
        var num = getRandom(1,videoCount);
        event.target.playVideoAt(num);
    }
}
function sendIds(ids)
{
    console.log(ids);
    // make what you want to do with them... ajax call to your php
}

相关内容

  • 没有找到相关文章

最新更新