2011-06-29 50 views
8

我有你的管链接像http://www.youtube.com/v/YR71GnQ4CU4?f=videos&app=youtube_gdata,然后如何将其转换为RTSP格式在VideoView中播放。如何获得RTSP链接Android

正在寻找GDATA API与此:http://gdata.youtube.com/feeds/api/videos?&max-results=20&v=2&format=1&q=“+ URLEncoder.encode(activity.criteria),但我不能找到如何获得相关的RTSP URL

+0

User919216 [这里]功能(http://www.youtube.com/watch?v=ZA7Kkhv30WA)是关于如何从YouTube上获取RTSP的简单视频需要45秒。 – KaSiris

+0

@KaSiris:不工作... –

回答

6

我得到了我的答案..thanx这个

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

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

在GDATA API只有我们收到此类型的链接:rtsp://v3.cache7.c.youtube.com/CiILENy73wIaGQlOCTh0GvUeYRMYDSANFEgGUgZ2aWRlb3MM/0/0/0/video.3gp

这些都打在VideoView

+7

嗨,你可以粘贴代码片段,你正在转换正常的YouTube网址到rtsp。 –

+0

@Udaykiran你可以发布整个代码片段,使用它生成一个rstp url。 – GrIsHu

1

钍可能会有点晚。这是有困难的人的一些工作代码。

try{ 
     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     Document doc = db.parse(new URL(url).openStream()); 
     doc.getDocumentElement().normalize(); 
     NodeList content = doc.getElementsByTagName("media:content"); 
     for(int i=0; i<content.getLength(); i++){ 
      Element rsp = (Element)content.item(i); 
      result.add(rsp.getAttribute("url")); 
     } 

    }catch(Exception e){ 
     Log.e("log_tag", "Error in http connection "+e.toString()); 
    } 
+0

“Q-GLuydiMe4”ID不会通过这种方式打开?任何想法 ?? –

+0

有时rtsp视频无法在手机中播放。您可能会解析错误的网址。你没有在videoview/mediaplayer中实现缓冲。没有更多信息,您将无法找到解决问题的解决方案。 – HarshMarshmallow

+0

当我通过这个视频ID解析数据时,在JSON中有一个现场应用程序$ Control。 有标签有字符串“限制”。所以,我认为这是视频无法播放的原因,但在iOS和其他Android应用中运行相同的ID。所以你有什么想法?为什么 ...??? –

0

下面是一个可以让你RTSP链接,YouTube视频

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); 
     Log.i(MyActivity.class.getSimpleName(), url.toString()); 
     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) 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; 
} 
+1

好吧...我剪下并粘贴并通过https://www.youtube.com/watch?v=Y6hz_s2XIAU,这是行不通的。我错过了什么吗? –

+0

我曾尝试过但没有工作。这个API似乎不推荐使用。 – Johnny