2

I am Developing an application which consists of video-view,Here is my problem

  1. when I have loaded normal url is not working,The URL of Youtube
  2. How to Convert the above URL to RTSP?
  3. the link which i have used http://www.youtube.com/watch?v=8mIOkkkA2jA

Code which i have tried:

VideoView videoView = (VideoView) findViewById(R.id.VideoView); 
MediaController mediaController = new MediaController(this); 
mediaController.setAnchorView(videoView); Uri video = Uri.parse("youtube.com/watch?v=8mIOkkkA2jA";); 
videoView.setMediaController(mediaController); 
videoView.setVideoURI(video); 
videoView.start();

Please guys can u explain me how to achieve this? Thanks in advance!!!!!!

Paresh Mayani
  • 127,700
  • 71
  • 241
  • 295
NikhilReddy
  • 6,904
  • 11
  • 38
  • 58
  • and the code which you have used and not working? – Paresh Mayani Nov 16 '11 at 10:27
  • But what is not working? if you paste here the code, it would be better to catch exact issue. – Paresh Mayani Nov 16 '11 at 10:29
  • VideoView videoView = (VideoView) findViewById(R.id.VideoView); MediaController mediaController = new MediaController(this); mediaController.setAnchorView(videoView); Uri video = Uri.parse("http://www.youtube.com/watch?v=8mIOkkkA2jA"); videoView.setMediaController(mediaController); videoView.setVideoURI(video); videoView.start(); – NikhilReddy Nov 16 '11 at 10:33
  • this is the code which i ave written it is not working for me... – NikhilReddy Nov 16 '11 at 10:34

3 Answers3

4

Note:Working only android mobile(Not in Tablet)

private class YourAsyncTask extends AsyncTask<Void, Void, Void>
    {
        ProgressDialog progressDialog;

        @Override
        protected void onPreExecute()
        {
            super.onPreExecute();
            progressDialog = ProgressDialog.show(AlertDetail.this, "", "Loading Video wait...", true);
        }

        @Override
        protected Void doInBackground(Void... params)
        {
            try
            {
                String url = "http://www.youtube.com/watch?v=1FJHYqE0RDg";
                videoUrl = getUrlVideoRTSP(url);
                Log.e("Video url for playing=========>>>>>", videoUrl);
            }
            catch (Exception e)
            {
                Log.e("Login Soap Calling in Exception", e.toString());
            }
            return null;
        }

        @Override
        protected void onPostExecute(Void result)
        {
            super.onPostExecute(result);
            progressDialog.dismiss();
/*
            videoView.setVideoURI(Uri.parse("rtsp://v4.cache1.c.youtube.com/CiILENy73wIaGQk4RDShYkdS1BMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp"));
            videoView.setMediaController(new MediaController(AlertDetail.this));
            videoView.requestFocus();
            videoView.start();*/            
            videoView.setVideoURI(Uri.parse(videoUrl));
            MediaController mc = new MediaController(AlertDetail.this);
            videoView.setMediaController(mc);
            videoView.requestFocus();
            videoView.start();          
            mc.show();
        }

    }

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());
            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;
    }
GrIsHu
  • 29,068
  • 10
  • 64
  • 102
Sock
  • 418
  • 1
  • 5
  • 12
  • May I ask you why this will not work on tablets, and just on Mobile Phones? – Mohammad Ersan Oct 14 '12 at 02:03
  • 1
    Q-GLuydiMe4 id will not open through this way ? i tried but cant convert into rtsp link but some are working fine. That video id running on the youtube perfectly. – Akarsh M Feb 24 '14 at 12:47
1

I used this code to get my in app YouTube videos to work : http://code.google.com/p/android-youtube-player/

It's doing exactly what you are looking for, so if you don't want all of the code, you can rip out the bit where it's inspecting the YouTube video stream and use it in your app.

Tooks me ages to find, hope it helps.

Martyn
  • 16,432
  • 24
  • 71
  • 104
1

Actual what i did was: code here

if (responseCode == HttpURLConnection.HTTP_OK) { 
                      InputStream in = httpConnection.getInputStream(); 

                      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                      DocumentBuilder db = dbf.newDocumentBuilder();

                      Document dom = db.parse(in);      
                      Element docEle = dom.getDocumentElement();

                      NodeList nl = docEle.getElementsByTagName("entry");
                      if (nl != null && nl.getLength() > 0) {
                        for (int i = 0 ; i < nl.getLength(); i++) {
                          Element entry = (Element)nl.item(i);
                          Element title = (Element)entry.getElementsByTagName("title").item(0);

                          String titleStr = title.getFirstChild().getNodeValue();

                          Element content = (Element)entry.getElementsByTagName("content").item(0);
                          String contentStr = content.getAttribute("src");

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

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

                         // System.out.println("content uri"+anotherurl);

                          Element photoUrl = (Element)entry.getElementsByTagName("media:thumbnail").item(0);
                          String photoStr = photoUrl.getAttribute("url");


                          JSONObject temp = new JSONObject();
                          try {
                            temp.put("Title", titleStr);
                            temp.put("Content", contentStr);
                            temp.put("PhotoUrl", photoStr);
                            temp.put("VideoUrl", anotherurl);
                          } catch (JSONException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                          }

                          collection.put(temp);

                          //VideoCell cell = new VideoCell(titleStr);
                        }
                      }
                    }

Here string named anotherurl is the url you looking for rtsp url.

Hope it helps.

Uday
  • 5,933
  • 9
  • 44
  • 76