如何获取播放列表影片fromYouTube [英] How to Fetch Playlists Videos fromYouTube

查看:151
本文介绍了如何获取播放列表影片fromYouTube的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个应用程序中,我想要的获取YouTube视频清单的,我能够做到这一点通过使用类似下面的链接特定用户的帐户:

  HttpUriRequest请求=新HTTPGET(http://gdata.youtube.com/feeds/api/users/GoogleDevelopers/uploads?v=2&alt=jsonc);
 

但如果我想获取YouTube视频清单使用播放列表,那么什么是我需要做的,我的code,喜欢这里的变化我不需要用户名获取视频。

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

原文链接: http://www.youtube.com/播放?列表= PL1D5B07DD840FB46D

我的code:

 公共类GetYouTubeUserVideosTask实现Runnable {

公共静态最终字符串库=库;
私人最终处理程序的replyTo;
私人最终字符串用户名;

公共GetYouTubeUserVideosTask(处理器的replyTo,字符串username){
    this.replyTo =的replyTo;
    this.username =用户名;
}

@覆盖
公共无效的run(){
    尝试 {

        HttpClient的客户端=新DefaultHttpClient();
        HttpUriRequest请求=新HTTPGET(http://gdata.youtube.com/feeds/api/playlists/PL1D5B07DD840FB46D?v=2&alt=json);
        HTT presponse响应= client.execute(要求);
        字符串jsonString = StreamUtils.convertToString(response.getEntity()的getContent());
        JSONObject的JSON =新的JSONObject(jsonString);
        JSONArray jsonArray = json.getJSONObject(数据)getJSONArray(项目)。
        名单<视频>视频=新的ArrayList<视频>();

        的for(int i = 0; I< jsonArray.length();我++){
            的JSONObject的JSONObject = jsonArray.getJSONObject(我);
            字符串标题= jsonObject.getString(标题);
            字符串URL;
            尝试 {
                URL = jsonObject.getJSONObject(玩家)的getString(默认)。
            }赶上(JSONException忽略){
                URL = jsonObject.getJSONObject(玩家)的getString(默认)。
            }

            字符串thumbUrl = jsonObject.getJSONObject(缩略图)的getString(sqDefault)。
            videos.add(新的视频(标题,URL,thumbUrl));
        }
        库LIB =新库(用户名,视频);

        捆绑数据=新包();
        data.putSerializable(图书馆,LIB);

        消息味精= Message.obtain();
        msg.setData(数据);
        replyTo.sendMessage(MSG);

    }赶上(ClientProtocolException E){
        Log.e(Feck,E);
    }赶上(IOException异常E){
        Log.e(Feck,E);
    }赶上(JSONException E){
        Log.e(Feck,E);
    }
}
   }
 

解决方案

我在一个项目中,我展示的所有视频从我的YouTube帐户,并在视频视图中显示它。工作就可以了。

有关从您的YouTube频道(帐号)播放的视频,你希望得到您的视频的RTSP URL。

这个例子的会给你最完美的想法。

另请参阅我的回答

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);
    }
}
   }

解决方案

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.

这篇关于如何获取播放列表影片fromYouTube的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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