对于洗牌API的Youtube播放列表修复 [英] Shuffle Playlist fix for Youtube API

查看:187
本文介绍了对于洗牌API的Youtube播放列表修复的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,当我试图在我的网站上创建一个嵌入式播放列表,并把它重新洗牌,我发现有是未固定的,不会让任何人使用随机播放功能的YouTube API中的错误

所以我想围绕创建工作。
我想我可以做一个随机数更换播放列表的索引号。
这工作得很好,让我洗牌的播放列表,只要我手动输入的最大和最小的随机数发生器。看到这里: http://jsfiddle.net/9kFyx/33/

虽然我很高兴这一点,我宁愿有一个更加动态的code,这将刚拿到的播放列表数组中元素的个数,并把它们作为一个最大号的随机因素。听起来很容易?
我抬起头来,这里的API参考:
https://developers.google.com/youtube/js_api_reference#Retrieving_playlist_information

和发现 player.getPlaylist()

我用 array.length; 来获得数字数组元素,并将其注入到随机生成。但是,这是行不通的!我不知道为什么。当我试图写的阵列,它回来为未定义。

您能帮我吗?

http://jsfiddle.net/42S5N/

 < D​​IV ID =玩家>< / DIV>
< D​​IV ID =输出的风格=宽度:300像素,高度:20像素;背景:白色;颜色:黑色;> MAXNUMBER< / DIV>
<脚本>
    var标记=使用document.createElement('脚本');    tag.src =htt​​ps://www.youtube.com/iframe_api;
    VAR firstScriptTag = document.getElementsByTagName('脚本')[0];
    firstScriptTag.parentNode.insertBefore(标签,firstScriptTag);    VAR的球员;    功能onYouTubeIframeAPIReady(){
        玩家=新YT.Player('球员',{
            高度:'411',
            宽度:'548',
            事件:{
                'onReady':onPlayerReady,
                    onStateChange':onPlayerStateChange
            }        });
    }
    VAR playlistArray;
    VAR playListArrayLength;
    VAR MAXNUMBER;    功能onPlayerReady(事件){
        player.loadPlaylist({
            listType:播放列表,
                '名单':'PLhaqVbVcY6UPkbjbeJM2CwRedCrr0rPJr
        });
        playlistArray = player.getPlaylist();
        playListArrayLength = playlistArray.length;
        MAXNUMBER = playListArrayLength;
        的document.getElementById('输出')的innerHTML = playListArrayLength。
        下一个();
    }    接下来的功能(){
        player.loadPlaylist({
            listType:播放列表,
                '名单':'PLhaqVbVcY6UPkbjbeJM2CwRedCrr0rPJr',
                索引:newRandomNumber(),
                'startSeconds':'0',
                suggestedQuality':'小'
        });
        player.setShuffle({
            shufflePlaylist':'真正的'
        });
    }
    变种oldNumber = 0;
    变种NewNumber = 0;    功能newRandomNumber(){
        oldNumber = NewNumber;
        NewNumber = Math.floor(的Math.random()* MAXNUMBER);
        如果(NewNumber == oldNumber){
            newRandomNumber();
        }其他{
            返回NewNumber;
        }
    }    功能onPlayerStateChange(事件){
        如果(Event.data中== YT.PlayerState.ENDED){
            下一个();
        }
    }
< / SCRIPT>


解决方案

我在PHP,有点势不可挡的解决方案,但它工作。

 函数getNumberofElements(){
    $ PID = $这个 - > GETPID();
    $ URL = \"https://www.googleapis.com/youtube/v3/playlists?part=contentDetails&id=$pid&key=$yourAPIKey\";
    $ CH = curl_init($网址);
    curl_setopt($ CH,CURLOPT_HTTPGET,TRUE);
    curl_setopt($ CH,CURLOPT_POST,FALSE);
    curl_setopt($ CH,CURLOPT_HEADER,FALSE);
    curl_setopt($ CH,CURLOPT_NOBODY,FALSE);
    curl_setopt($ CH,CURLOPT_VERBOSE,FALSE);
    curl_setopt($ CH,CURLOPT_REFERER,);
    curl_setopt($ CH,CURLOPT_FOLLOWLOCATION,TRUE);
    curl_setopt($ CH,CURLOPT_MAXREDIRS,4);
    curl_setopt($ CH,CURLOPT_RETURNTRANSFER,TRUE);
    curl_setopt($ CH,CURLOPT_USERAGENT,Mozilla的/ 5.0(视窗; U; Windows NT的6.1;他; RV:1.9.2.8)的Gecko / 20100722火狐/ 3.6.8);    $ numberOfElements = curl_exec($ CH); // $ XML = simplexml_load_string($ numberOfElements);    $ numberOfElements = SUBSTR(的strstr($ numberOfElements,'ITEMCOUNT:'),strlen的('ITEMCOUNT:'));
    $ numberOfElements = str_replace函数(},,$ numberOfElements);
    $ numberOfElements = str_replace函数(],,$ numberOfElements);    返回$ numberOfElements;
}

So while I was trying to create an embedded playlist on my website, and get it to shuffle, I found out that there is a bug in the YouTube API that wasn't fixed and won't let anyone use the Shuffle functionality.

So I'm trying to create a work around. I figured I could make a Random number replace the index number of the playlist. That worked fine, and let me shuffle the playlist, as long as I manually entered the Max and Min for the Random number generator. see it here: http://jsfiddle.net/9kFyx/33/

While I'm very happy with this, I would much rather to have a more dynamic code, which would just get the number of Elements in the playlist array, and use them as a Max number for the Random factor. Sounds easy enough? I looked up the API reference for that here: https://developers.google.com/youtube/js_api_reference#Retrieving_playlist_information

and found player.getPlaylist()

I used array.length; to get the number array elements, and inject it into the random generator. But it doesn't work! I'm not sure why. When I tried to write the array, it came back as undefined.

Can you help?

http://jsfiddle.net/42S5N/

<div id="player"></div>
<div id="output" style="width:300px; height:20px; background:white; color:black;">maxNumber</div>
<script>
    var tag = document.createElement('script');

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

    var player;

    function onYouTubeIframeAPIReady() {
        player = new YT.Player('player', {
            height: '411',
            width: '548',
            events: {
                'onReady': onPlayerReady,
                    'onStateChange': onPlayerStateChange
            }

        });
    }


    var playlistArray;
    var playListArrayLength;
    var maxNumber;

    function onPlayerReady(event) {
        player.loadPlaylist({
            'listType': 'playlist',
                'list': 'PLhaqVbVcY6UPkbjbeJM2CwRedCrr0rPJr'
        });
        playlistArray = player.getPlaylist();
        playListArrayLength = playlistArray.length;
        maxNumber = playListArrayLength;
        document.getElementById('output').innerHTML = playListArrayLength;
        next();
    }



    function next() {
        player.loadPlaylist({
            'listType': 'playlist',
                'list': 'PLhaqVbVcY6UPkbjbeJM2CwRedCrr0rPJr',
                'index': newRandomNumber(),
                'startSeconds': '0',
                'suggestedQuality': 'small'
        });
        player.setShuffle({
            'shufflePlaylist': 'true'
        });
    }


    var oldNumber = 0;
    var NewNumber = 0;

    function newRandomNumber() {
        oldNumber = NewNumber;
        NewNumber = Math.floor(Math.random() * maxNumber);
        if (NewNumber == oldNumber) {
            newRandomNumber();
        } else {
            return NewNumber;
        }
    }

    function onPlayerStateChange(event) {
        if (event.data == YT.PlayerState.ENDED) {
            next();
        }
    }
</script>

解决方案

My solution in PHP, a little overwhelming, but it works.

function getNumberofElements() {
    $pid = $this->getPID();
    $url = "https://www.googleapis.com/youtube/v3/playlists?part=contentDetails&id=$pid&key=$yourAPIKey";
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_HTTPGET, TRUE);
    curl_setopt($ch, CURLOPT_POST, FALSE);
    curl_setopt($ch, CURLOPT_HEADER, false);
    curl_setopt($ch, CURLOPT_NOBODY, FALSE);
    curl_setopt($ch, CURLOPT_VERBOSE, FALSE);
    curl_setopt($ch, CURLOPT_REFERER, "");
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
    curl_setopt($ch, CURLOPT_MAXREDIRS, 4);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; he; rv:1.9.2.8) Gecko/20100722 Firefox/3.6.8");

    $numberOfElements = curl_exec($ch);  // $xml = simplexml_load_string($numberOfElements);

    $numberOfElements = substr(strstr($numberOfElements, 'itemCount":'), strlen('itemCount":'));
    $numberOfElements = str_replace("}", " ", $numberOfElements);
    $numberOfElements = str_replace("]", " ", $numberOfElements);

    return $numberOfElements;
}

这篇关于对于洗牌API的Youtube播放列表修复的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆