How to get a youtube playlist using javascript API and json

后端 未结 3 1979
后悔当初
后悔当初 2020-12-07 18:09

This is part of my youtube project. I try to extract video information from JSON format but I have problem in this line:

var videoId = data.feed.entry[i].li         


        
3条回答
  •  無奈伤痛
    2020-12-07 18:57

    I found that these lines:

    var feedURL = item.link[1].href;
    var fragments = feedURL.split("/");
    var videoID = fragments[fragments.length - 2];
    

    are expecting item.link[1].href to be in this format:

    http://gdata.youtube.com/feeds/api/videos/NAs5it-xjnQ/responses?v=2
    

    However, it does not necessarily work as sometimes item.link[1] gives an URL like

    http://www.youtube.com/watch?v=Vcp7xz6dfWE&feature=youtube_gdata
    

    Fragments[fragments.length - 2] will end up being "www.youtube.com" instead of the video ID.

    I modified it to retrieve the link from item.content.src which always has a fixed format in the URL e.g.

    http://www.youtube.com/v/NAs5it-xjnQ?version=3&f=playlists&app=youtube_gdata
    

    So the final code snippet is something like:

    var tmp = item.content.src.split("/").reverse()[0];
    var videoID = tmp.substring(0, tmp.indexOf("?"));
    

    which so far has not failed me yet.

    Hope this helps those who are stuck or is having problems retrieving the video ID.

    Regards
    CK

提交回复
热议问题