Example YouTube Playlist Code?

白昼怎懂夜的黑 提交于 2019-12-21 22:10:57

问题


I'd like to create a playlist that loads all the videos a specific user has uploaded.

I was suggested to use the following code (assuming the account to pull in is YouTube):

loadPlaylist( { listType: 'user_uploads', list: 'youtube' } );

I have looked over the API pages as well: http://code.google.com/apis/youtube/js_api_reference.html

But I can't find an actual example code that uses load playlist. Being completely new to YouTube API I have no idea what type of wrapper code I need to make the above work. Something like this (of course I'm missing parts):

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/swfobject/2.2/swfobject.js"></script>    
<script type="text/javascript">
loadPlaylist( { listType: 'user_uploads', list: 'youtube' } );
</script>

Or if someone could provide working example with the original loadplaylist line that would be great, and I can work on the other details I need on my own from there.

-YouTube API Newbie


回答1:


First of all , I suggest you to use IFRAME embed style, because it work on both desktop and mobile (iOS,Android,BB,Windows, very nice). The sample code is at Youtube IFRAME embed . This one contain the html code for you to get started.

Let's get into your question.

1 Let assume you get youtube player as

player = new YT.Player('player', {
      height: '390',
      width: '640',
        videoId: 'u1zgFlCw8Aw',
      events: {
        'onReady': onPlayerReady
      }
});

don't care with videoId. just insert any valid youtube video's ID.

notice that we register 'onReady': onPlayerReady

2 Load your playlist into the player using onPlayerReady

function onPlayerReady(event) { 
    event.target.loadPlaylist({list: "UUPW9TMt0le6orPKdDwLR93w", index: 1, startSeconds: 10,suggestedQuality: "small"});
}

You can read more on Youtube JSAPI reference Hope this work on you. ^^.


UPDATE

you can also specify the playlist in playerVars object.

function onYouTubePlayerAPIReady() {
        player = new YT.Player('player', {
          height: '390',
          width: '640',
        videoId: 'u1zgFlCw8Aw',
        playerVars: {
          listType:'playlist',
          list: 'UUPW9TMt0le6orPKdDwLR93w'
        },
          events: {
            'onReady': onPlayerReady,
            'onStateChange': onPlayerStateChange
          }
        });
      }


来源:https://stackoverflow.com/questions/9313216/example-youtube-playlist-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!