0

I use this api codes to play youtube videos in android videoview.It always says me does not play.And I use many ways to open it.I look in stackoverflow.com for many questions about that but I still do not understand.The last one of my solutions for this problem is this.I use these methods :

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");
        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) {
        return urlYoutube;
      }  
    }
    public static String extractYoutubeId(String url) throws MalformedURLException {
        String query = new URL(url).getQuery();
        String[] param = query.split("&");
        String id = null;
        for (String row : param) {
          String[] param1 = row.split("=");
          if (param1[0].equals("v")) {
            id = param1[1];
          }
        }
        return id;
      }

and this codes which in my play_video_button_on_click_event :

VideoView videoView = (VideoView) findViewById(R.id.videoView1);          
        String urlYoutube = "http://www.youtube.com/watch?v=dJhRW_vg-dI";
        String urlRtsp = getUrlVideoRTSP(urlYoutube);//Il metodo che abbiamo visto
        videoView.setVideoURI(Uri.parse(urlRtsp));
        MediaController controller = new MediaController(CaillouizleActivity.this);
        videoView.setMediaController(controller);
        videoView.start();  

But it still error : this video can't be play. What can I do? Thanks...

rgksugan
  • 3,428
  • 12
  • 44
  • 53
Ersin Gülbahar
  • 6,713
  • 15
  • 60
  • 114
  • what is the rstp url you open? this one rtsp://v4.cache6.c.youtube.com/CiILENy73wIaGQnS-eD7W1GYdBMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp ? – njzk2 Mar 23 '12 at 11:41

0 Answers0