How to Play Youtube Video on VideoView?
How to Play Youtube Video on VideoView?
I have fetched many youtube video url's using rss(xml Parsing) from the following url:-
http://gdata.youtube.com/feeds/base/videos/-/jus开发者_如何学Ctinbieber?orderby=published&alt=rss&client=ytapi-youtube-rss-redirect&v=2
but the problem is this that http link is not played in videoview.
Please Help me.
You want to convert your gdata URL into rtsp format. Following function convert your URL into rtsp format.
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;
}
And the complete example show how to get videos from Youtube channel in listview is click here
精彩评论