如何获取 RTSP 链接 Android

发布于 2024-11-17 17:07:08 字数 483 浏览 2 评论 0原文

我有 YouTube 链接,例如 http://www.youtube.com/v/YR71GnQ4CU4?f =videos&app=youtube_gdata ,那么如何转换为RTSP格式在VideoView中播放。

正在搜索 gdata api:http: //gdata.youtube.com/feeds/api/videos?&max-results=20&v=2&format=1&q="+ URLEncoder.encode(activity.criteria) 但我找不到如何获取相关的 RTSP url。

Am having you-tube links like http://www.youtube.com/v/YR71GnQ4CU4?f=videos&app=youtube_gdata , then how to convert it to RTSP format to play in VideoView.

Am searching gdata api with this: http://gdata.youtube.com/feeds/api/videos?&max-results=20&v=2&format=1&q="+ URLEncoder.encode(activity.criteria) but i cant find how to get related RTSP url.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

不再让梦枯萎 2024-11-24 17:07:08

我得到了我的答案..thanx

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< /code>

这些正在 VideoView 中播放。

I got my answer ..thanx to this

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

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

In gdata api only we are getting this type of links : rtsp://v3.cache7.c.youtube.com/CiILENy73wIaGQlOCTh0GvUeYRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp

These are playing in VideoView.

少女净妖师 2024-11-24 17:07:08

这可能有点晚了。这是一些为遇到麻烦的人提供的工作代码。

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

This might be a little late. Here is some working code for people having trouble.

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());
    }
忆伤 2024-11-24 17:07:08

以下是可以为您获取 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;
}

Below is the function which can get you RTSP link for the youtube video

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;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文