2011-08-04 261 views

回答

2

您想将您的gdata网址转换为rtsp格式。以下功能将您的网址转换为rtsp格式。

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

而完整的例子展示了如何从Youtube频道如果你解决它,然后接受的答案里面的视频在列表视图中click here

+0

hello hardik,在这个例子中,视频在webview中播放而不是mediaplayer,我想在mediaplayer中播放youtube视频。 –

+0

创建一个活动并将视频的rtsp URL(用户单击的特定视频的URL)作为包传递,然后编写波纹管代码。 '捆绑捆= getIntent()getExtras();' \t \t'最终字符串数据= bundle.getString( “视频ID”);' \t \t'最终VideoView VV =(VideoView)findViewById(R.id.VideoView) ;' \t \t'的MediaController MC =新的MediaController(本);' \t \t'mc.setEnabled(真);' \t \t'mc.show(0);' \t \t'vv.setMediaController( mc);' \t \t'vv.setVideoURI(Uri.parse(getUrlVideoRTSP(da ta)));' –

+0

你试过这个吗? –

相关问题