0

i want to have a youtube player in my app, where i can adjust the url of youtube like http://www.youtube.com/watch?v="yt_id" the yt_id is a string.

i have tryed webview's, but i get a loading screen of two seconds and then a black screen

i have tryed videoview, but yout need a RTSP code from youtube but i can't find a good code the can change a youtube stadard url to RTSP.

it isn't an option to go to the youtube app it self for variouse reasons.

and the api 3.0 isn't out jet.

can anyone plz help me, thanks in advance.

Pieter Tielemans
  • 624
  • 5
  • 15

2 Answers2

0

Same issues with me, But now youtube is working fine for me.

Check following link.

YouTube Video not playing in WebView - Android

Community
  • 1
  • 1
Ranjitsingh Chandel
  • 1,479
  • 6
  • 19
  • 33
0

You can follow this code to get the RTSP link of an 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 );
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            Document doc = documentBuilder.parse(connection.getInputStream());
            org.w3c.dom.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;

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

videoUrl = getUrlVideoRTSP(youtube_url);

After getting the RTSP link you can play that link in a video view. Hope it will work fine for you as it is worked in my application. Thanks.

Santanu
  • 141
  • 2
  • 11