【问题标题】:android youtube player安卓 youtube 播放器
【发布时间】:2012-10-10 12:56:06
【问题描述】:
我想在我的应用程序中有一个 youtube 播放器,我可以在其中调整 youtube 的 url,例如 http://www.youtube.com/watch?v="yt_id" yt_id 是一个字符串。
我已经尝试过 webview,但我得到了两秒钟的加载屏幕,然后是黑屏
我尝试过 videoview,但您需要来自 youtube 的 RTSP 代码,但我找不到可以将 youtube 标准 url 更改为 RTSP 的好代码。
由于各种原因,无法自行访问 youtube 应用程序。
而且 api 3.0 还没有推出。
谁能帮帮我,在此先感谢。
【问题讨论】:
标签:
java
android
youtube
youtube-api
【解决方案2】:
您可以按照此代码获取 youtube 视频的 RTSP 链接
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 );
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
Document doc = documentBuilder.parse(connection.getInputStream());
org.w3c.dom.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;
}
protected 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;
}
videoUrl = getUrlVideoRTSP(youtube_url);
获得 RTSP 链接后,您可以在视频视图中播放该链接。
希望它在我的应用程序中可以正常工作。
谢谢。