如何在VideoView上播放Youtube视频?
我使用 rss(xml 解析)从以下 url 获取了许多 youtube 视频 url:-
但问题是 http 链接没有在 videoview 中播放。
请帮助我。
最佳答案
您想将 gdata URL 转换为 rtsp 格式。以下函数将您的 URL 转换为 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;
}
完整的示例显示了如何在 ListView 中从 Youtube channel 获取视频是 click here
关于android - 如何在 VideoView 上播放 Youtube 视频?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6937615/