如何在VideoView上播放Youtube视频?

时间:2011-08-04 07:21:04

标签: android

如何在VideoView上播放Youtube视频?

我使用rss(xml Parsing)从以下网址获取了许多YouTube视频网址

http://gdata.youtube.com/feeds/base/videos/-/justinbieber?orderby=published&alt=rss&client=ytapi-youtube-rss-redirect&v=2

但问题是这个http链接没有在videoview中播放。

Plaese帮助我。

1 个答案:

答案 0 :(得分:2)

您想将gdata网址转换为rtsp格式。以下功能将您的URL转换为rtsp格式。

public static String getUrlVideoRTSP(String urlYoutube) {
    try {
        String gdy = "http://gdata.youtube.com/feeds/base/videos/-/justinbieber?orderby=published&alt=rss&client=ytapi-youtube-rss-redirect&v=2";
        DocumentBuilder documentBuilder = DocumentBuilderFactory
                .newInstance().newDocumentBuilder();
        String id = extractYoutubeId(urlYoutube);
        URL url = new URL(gdy + id);
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        Document doc = documentBuilder.parse(connection.getInputStream());
        Element el = doc.getDocumentElement();
        NodeList list = el.getElementsByTagName("media:content");// /media:content
        String cursor = urlYoutube;
        for (int i = 0; i < list.getLength(); i++) {
            Node node = list.item(i);
            if (node != null) {
                NamedNodeMap nodeMap = node.getAttributes();
                HashMap<String, String> maps = new HashMap<String, String>();
                for (int j = 0; j < nodeMap.getLength(); j++) {
                    Attr att = (Attr) nodeMap.item(j);
                    maps.put(att.getName(), att.getValue());
                }
                if (maps.containsKey("yt:format")) {
                    String f = maps.get("yt:format");
                    if (maps.containsKey("url")) {
                        cursor = maps.get("url");
                    }
                    if (f.equals("1"))
                        return cursor;
                }
            }
        }
        return cursor;
    } catch (Exception ex) {
        Log.e("Get Url Video RTSP Exception======>>", ex.toString());
    }
    return urlYoutube;
}

private static String extractYoutubeId(String url) {
    return url;
} 

完整示例显示如何从列表视图中的Youtube频道获取视频click here