如何获得RTSP链接Android

时间:2011-06-29 12:02:26

标签: android youtube rtsp android-videoview

我有{-3}}之类的管连接,然后如何将其转换为RTSP格式以在VideoView中播放。

我用这个搜索gdata api:http://www.youtube.com/v/YR71GnQ4CU4?f=videos&app=youtube_gdata“+ URLEncoder.encode(activity.criteria)但是我找不到如何获取相关的RTSP网址。

3 个答案:

答案 0 :(得分:6)

我得到了答案..谢谢这个

Element rsp = (Element)entry.getElementsByTagName("media:content").item(1);

                              String anotherurl=rsp.getAttribute("url");

在gdata api中,我们只有这种类型的链接:rtsp://v3.cache7.c.youtube.com/CiILENy73wIaGQlOCTh0GvUeYRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp

这些是在VideoView中播放的。

答案 1 :(得分:1)

这可能有点晚了。这是一些有问题的人的工作代码。

try{
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new URL(url).openStream());
        doc.getDocumentElement ().normalize ();
        NodeList content = doc.getElementsByTagName("media:content");
        for(int i=0; i<content.getLength(); i++){
            Element rsp = (Element)content.item(i);
            result.add(rsp.getAttribute("url"));
        }

    }catch(Exception e){
        Log.e("log_tag", "Error in http connection "+e.toString());
    }

答案 2 :(得分:0)

以下是可以为youtube视频提供RTSP链接的功能

public static String getUrlVideoRTSP(String urlYoutube) {
    try {
        String gdy = "http://gdata.youtube.com/feeds/api/videos/";
        DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        String id = extractYoutubeId(urlYoutube);
        URL url = new URL(gdy + id);
        Log.i(MyActivity.class.getSimpleName(), url.toString());
        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) throws MalformedURLException {
    String id = null;
    try {
        String query = new URL(url).getQuery();
        if (query != null) {
            String[] param = query.split("&");
            for (String row : param) {
                String[] param1 = row.split("=");
                if (param1[0].equals("v")) {
                    id = param1[1];
                }
            }
        } else {
            if (url.contains("embed")) {
                id = url.substring(url.lastIndexOf("/") + 1);
            }
        }
    } catch (Exception ex) {
        Log.e("Exception", ex.toString());
    }
    return id;
}