8

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.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
Uday
  • 5,933
  • 9
  • 44
  • 76

3 Answers3

6

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.

Uday
  • 5,933
  • 9
  • 44
  • 76
0

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());
    }
HarshMarshmallow
  • 1,007
  • 11
  • 23
  • "Q-GLuydiMe4" id will not open through this way ? Any idea ?? – Akarsh M Feb 24 '14 at 11:23
  • Sometimes rtsp videos can't be played in mobile. You might have the wrong url parsed. You didn't implement buffering in the videoview/mediaplayer. Without more information, you are going to have trouble finding a solution for your problem. – HarshMarshmallow Feb 24 '14 at 20:35
  • When I parse the data through this video id , In JSON there is a field app$Control. And there is tag is having the string "Restricted". So, I think that is reason for video can't be played but the same id is running in iOS and other Android Apps. So have you any idea ??? why is it ...??? – Akarsh M Feb 25 '14 at 05:02
  • The same id is running for android and iOS apps because it is pointing to a youtube video and not anything specific for the mobile platform. I would recommend posting your actual error message with a well thought out question to stackoverflow instead of having it buried in the comments of this thread. – HarshMarshmallow Feb 25 '14 at 17:08
  • Can you please provide me full code for RTSP ... Because I am trying from a long time but not getting the result, So would you please help me out from this thing – Akarsh M Feb 25 '14 at 17:45
  • This code shows you how to get the links. Not what to do with them afterwards. I am pretty busy at the moment and dont have enough time to go back into this code and format it in a way that would make sense for you. If you have an actual question please start a new thread. People usually respond quickly and are generally helpful. – HarshMarshmallow Feb 25 '14 at 18:13
0

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;
}
Nixit Patel
  • 4,435
  • 5
  • 30
  • 57