Liza
Liza

Reputation: 227

How to Fetch Playlists Videos fromYouTube

I am writing an app in which i want to Fetch List of YouTube Videos, and i am able to do that by using a particular user's account like the below link:

 HttpUriRequest request = new HttpGet("http://gdata.youtube.com/feeds/api/users/GoogleDevelopers/uploads?v=2&alt=jsonc");

but if i want to fetch list of YouTube videos using playlist, so what are the changes i need to do in my code, like here i no need username to fetch videos.

  http://gdata.youtube.com/feeds/api/playlists/PL1D5B07DD840FB46D?v=2&alt=json

original link: http://www.youtube.com/playlist?list=PL1D5B07DD840FB46D

My Code:

  public class GetYouTubeUserVideosTask implements Runnable {

public static final String LIBRARY = "Library";
private final Handler replyTo;
private final String username;

public GetYouTubeUserVideosTask(Handler replyTo, String username) {
    this.replyTo = replyTo;
    this.username = username;
}

@Override
public void run() {
    try {

        HttpClient client = new DefaultHttpClient();
        HttpUriRequest request = new HttpGet("http://gdata.youtube.com/feeds/api/playlists/PL1D5B07DD840FB46D?v=2&alt=json");   
        HttpResponse response = client.execute(request);
        String jsonString = StreamUtils.convertToString(response.getEntity().getContent());
        JSONObject json = new JSONObject(jsonString);
        JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");
        List<Video> videos = new ArrayList<Video>();

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            String title = jsonObject.getString("title");
            String url;
            try {
                url = jsonObject.getJSONObject("player").getString("default");
            } catch (JSONException ignore) {
                url = jsonObject.getJSONObject("player").getString("default");
            }

            String thumbUrl = jsonObject.getJSONObject("thumbnail").getString("sqDefault"); 
            videos.add(new Video(title, url, thumbUrl));
        }       
        Library lib = new Library(username, videos);

        Bundle data = new Bundle();
        data.putSerializable(LIBRARY, lib);

        Message msg = Message.obtain();
        msg.setData(data);
        replyTo.sendMessage(msg);

    } catch (ClientProtocolException e) {
        Log.e("Feck", e);
    } catch (IOException e) {
        Log.e("Feck", e);
    } catch (JSONException e) {
        Log.e("Feck", e);
    }
}
   }

Upvotes: 2

Views: 5366

Answers (2)

Hardik Joshi
Hardik Joshi

Reputation: 9507

I worked on it in my one project in which I display all videos from my youtube account and display it in video view.

For playing video from your youtube channel(account) you want to get rtsp URL of your video.

Follow this example it will give you perfect idea.

Also refer My answer.

Upvotes: 1

Matias Molinas
Matias Molinas

Reputation: 2296

You can use the new YouTube Data API v3 to get playlists:

https://developers.google.com/youtube/v3/docs/playlists

In this tutorial that I wrote (in Spanish .. but you can see the code changes..):

http://fuse21.blogspot.com.ar/2013/01/utilizando-youtube-data-api-v3-desde.html

I show how to change the official sample "tasks-android-sample":

http://code.google.com/p/google-api-java-client/source/browse/tasks-android-sample/?repo=samples

to use the YouTube Data API instead of the Tasks API.

Upvotes: 1

Related Questions