我如何在videoview中播放youtube视频?

时间:2012-03-23 11:37:08

标签: android android-videoview android-video-player

我使用这个api代码在android videoview中播放youtube视频。它总是说我不玩。我用很多方法打开它。我在stackoverflow.com看了很多关于那个问题,但我还是不明白我对这个问题的最后一个解决方案就是这个。我使用这些方法:

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

这个代码在我的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();  

但它仍然是错误:此视频无法播放。我能做什么? 感谢...

0 个答案:

没有答案