Ads

Wednesday, August 1, 2012

YouTube Playlist in Titanium

In this post I am going to discuss about how to fetch YouTube playlist from a channel using Appcelerator Titanium.

YouTube offer API for category, search term, playlist, favorites, subscriptions... etc. Here you can found list of YouTube API
https://developers.google.com/youtube/getting_started

Below API will retrieve the user's playlists. This request does not require authentication
https://gdata.youtube.com/feeds/api/users/userId/playlists?v=2
* Here  userId represents YouTube username.

The above API response containing a playlists feed in XML format.
The response contains a series of <entry>  tags, with each entry describing a playlist.
Each entry contains the playlist's title, description, author and modification date as well as
<content> tag that specifies the URL for retrieving the list of videos in the playlist

Here I am  using Apple's YouTube username to fetch their YouTube playlist
var channelName = "apple";
var loader = Titanium.Network.createHTTPClient();
loader.open("GET", "http://gdata.youtube.com/feeds/api/users/"+channelName+"/playlists?v=2");

loader.onload = function() {
 var doc = this.responseXML.documentElement;
 var items = doc.getElementsByTagName("entry");
 for (var i = 0; i < items.length; i++) {
   var item = items.item(i);
   var playlist_title = item.getElementsByTagName("title").item(0).text;//playlist title
   var vid_count = item.getElementsByTagName("yt:countHint").item(0).text; //video count for this playlist
   var play_list_id = item.getElementsByTagName("yt:playlistId").item(0).text; //play_list_id
 }
};

loader.onerror = function(e) {
 alert("Error:"+e.error);
};

loader.timeout = 10000;
loader.send();

In below code I am going to retrieve all the videos in a particular playlist using playlist id.
http://gdata.youtube.com/feeds/api/playlists/playlist_id?v=2
var loader = Titanium.Network.createHTTPClient();
loader.open("GET", "http://gdata.youtube.com/feeds/api/playlists/" + playlist_id + "?v=2");

loader.onload = function() {
  var doc = this.responseXML.documentElement;
  var items = doc.getElementsByTagName("entry");
  for (var c = 0; c < items.length; c++) {
   var item = items.item(c);
   var thumbnails = item.getElementsByTagName("media:thumbnail");
   if (thumbnails && thumbnails.length > 0) {
     var media = thumbnails.item(0).getAttribute("url"); //thumbnail image
     var title = item.getElementsByTagName("title").item(0).text; //video title
     var videoId = item.getElementsByTagName("yt:videoid").item(0).text; //video id
   }
  }
};

loader.onerror = function(e) {
 alert("Error:" + e.error);
};

loader.timeout = 5000;
loader.send(); 

YouTube Playlist in Titanium
Playlist listing for Apple

YouTube Playlist in Titanium
Video List for Particular Playlist
Here you can download the complete working project from my Github Titanium YouTube Playlist

CodeStrong

8 comments:

  1. Can I use this code for a specific chanel/playlist?

    ReplyDelete
  2. Yes, you can. Change the Channel Name here // var channelName = "apple";

    ReplyDelete
  3. Mine has an error playlist_id line 27

    ReplyDelete
  4. Thanks for this tutorial. Short and sweet and to the point :)

    ReplyDelete
  5. Hi Karthi,its a wonderful tutorial.Helped me a lot in my project.I checked the option for playing videos from a particular channel.I was wondering how can we play a particular playlist or videos of a user on youtube.I am sure there might be some functionality already there in your code, I just need guidance how to use it for playlist and user videos. Thanks

    ReplyDelete
  6. this code is not working for android can you please help

    ReplyDelete
  7. stackoverflow_littleJune 13, 2014 at 3:23 AM

    I have donwloaded Github project, changed the channelName but i get an error : Unable to connect to the server. I m sorry but im im new to titanium....for what is needed the cloud setted up here ? I see also highlighted errors in the code.... can you give me an hand to setup this script pls ? I wont bother you for more than 5 mins , Contact me on the email please, ill make a donation.

    ReplyDelete
  8. Hi Karthi, I m making an app to search Youtube directly from the app Searchbar, for that I m looking for a related API, can you plz tell me where did u download this API from? Or can u help me in some other way for this?

    ReplyDelete