1

How to Play Youtube Video on VideoView?

I have fetched many youtube video url's using rss(xml Parsing) from the following url:-

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

but the problem is this that http link is not played in videoview.

Please Help me.

Nancy Kanwar
  • 157
  • 2
  • 16
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128

1 Answers1

2

You want to convert your gdata URL into rtsp format. Following function convert your URL into rtsp format.

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

And the complete example show how to get videos from Youtube channel in listview is click here

Alp Altunel
  • 3,324
  • 1
  • 26
  • 27
Hardik Joshi
  • 9,477
  • 12
  • 61
  • 113
  • hello hardik, in this example the video is playing in webview not mediaplayer and i want to play youtube video in mediaplayer. – Dipak Keshariya Jun 25 '12 at 07:32
  • Create one activity and pass rtsp URL of video(URL of particular video in which user click) as bundle and then write bellow code. `Bundle bundle = getIntent().getExtras();` `final String data = bundle.getString("videoid");` `final VideoView vv = (VideoView) findViewById(R.id.VideoView);` `MediaController mc = new MediaController(this);` `mc.setEnabled(true);` `mc.show(0);` `vv.setMediaController(mc);` `vv.setVideoURI(Uri.parse(getUrlVideoRTSP(data)));` – Hardik Joshi Jun 25 '12 at 07:41