【发布时间】:2011-08-04 07:21:03
【问题描述】:
如何在 VideoView 上播放 Youtube 视频?
我使用 rss(xml Parsing) 从以下网址获取了许多 youtube 视频网址:-
但问题是 http 链接没有在 videoview 中播放。
请帮帮我。
【问题讨论】:
-
如果你解决了然后接受答案。
标签: android
如何在 VideoView 上播放 Youtube 视频?
我使用 rss(xml Parsing) 从以下网址获取了许多 youtube 视频网址:-
但问题是 http 链接没有在 videoview 中播放。
请帮帮我。
【问题讨论】:
标签: android
您想将您的 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;
}
完整的例子展示了如何在列表视图中从 Youtube 频道获取视频是click here
【讨论】:
Bundle bundle = getIntent().getExtras();final String data = bundle.getString("videoid");final VideoView vv = (VideoView) findViewById(R.id.VideoView);MediaController mc = new MediaController(this);mc.setEnabled(true);mc.show(0);vv.setMediaController(mc);vv.setVideoURI(Uri.parse(getUrlVideoRTSP(data)));