使用播放列表网址获取youtube视频

时间:2012-12-31 09:29:31

标签: android android-emulator youtube youtube-api

如何从http://gdata.youtube.com/feeds/api/playlists/EL5BQGc0nPiVI?v=2&alt=json

获取视频列表

我制作了一个程序,我可以使用这种网址来获取视频列表:请检查this但这次我想在Android中使用播放列表获取视频列表使用this网址,实际的YouTube视频URL

这里我使用的是JSON,但现在我不知道我需要做什么更改才能使用上面的播放列表网址获取视频列表。

  @Override
public void run() {
    try {

        HttpClient client = new DefaultHttpClient();

        HttpUriRequest request = new HttpGet("http://gdata.youtube.com/feeds/api/playlists/EL5BQGc0nPiVI?v=2&alt=json"); 





        // Get the response that YouTube sends back
        HttpResponse response = client.execute(request);
        // Convert this response into a readable string
    String jsonString = StreamUtils.convertToString
            (response.getEntity().getContent());
        // Create a JSON object that we can use from the String
        JSONObject json = new JSONObject(jsonString);



        // Get are search result items
    JSONArray jsonArray = json.getJSONObject("data").getJSONArray("items");

        // Create a list to store are videos in
        List<Video> videos = new ArrayList<Video>();

        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            // The title of the video
            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);
    }
}

2 个答案:

答案 0 :(得分:1)

 String JKYouTubeActivity.YOUTUBE_INFO_URL=http://gdata.youtube.com/feeds/api/playlists/_ID_?v=2&alt=json

 private String getUrl(String id) throws IOException, JSONException {
        HttpClient client = new DefaultHttpClient();
        HttpGet clientGetMethod = new HttpGet(JKYouTubeActivity.YOUTUBE_INFO_URL.replace("_ID_", id));
        HttpResponse clientResponse = null;
        clientResponse = client.execute(clientGetMethod);
        String infoString = _convertStreamToString(clientResponse.getEntity().getContent());
        String urldata=new JSONObject(infoString).getJSONObject("entry").getJSONObject("media$group").getJSONArray("media$content").getJSONObject(0).getString("url");
        return new JSONObject(infoString).getJSONObject("entry").getJSONObject("media$group").getJSONArray("media$content").getJSONObject(0).getString("url");
    }

    private String _convertStreamToString(InputStream iS) {
        BufferedReader reader = new BufferedReader(new InputStreamReader(iS));
        StringBuilder sB = new StringBuilder();
        String line = null;
        try {
            while ((line = reader.readLine()) != null)
            {
                sB.append(line).append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                iS.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return sB.toString();
    }

}

获取urldata之后,您可以将其流式传输或执行任何您想要的操作。同样你也可以获得缩略图和标题

答案 1 :(得分:0)

您可以使用recyclerView作为列表。我用Youtube api休息电话完成了这个。您需要从Google开发者控制台获取浏览器密钥。我在这里的答案中解释了这一点:

https://stackoverflow.com/a/41201084/3689744

&#34;新方式&#34; Youtube休息电话会看起来像这样(我相信最高结果是50):

https://www.googleapis.com/youtube/v3/playlistItems?part=snippet&playlistId=iBi9LVIrC-fVelw2-I2r-yrEk6SpXfO8&key=AIzaSyCI1oCTXwZzgVv7LDQ8NykSIUEWt247KnU&maxResults=50