我搜索了很多以获取YouTube视频的流URL,但它们都无法正常工作。YouTube是否删除了Android视频中流媒体视频的支持?
代码样本:
public String getYoutubeUriForVideoId(String videoId) {
String url = "";
try {
Log.e(TAG, "Input video id: "+videoId);
String YOUTUBE_VIDEO_INFORMATION_URL =
"http://m.youtube.com/watch?ajax=1&layout=mobile&tsp=1&utcoffset=330&v="+videoId;
String USER_AGENT ="Mozilla/5.0 (Linux; U; Android 2.2.1; en-gb; GT-I9003 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1";
Log.e(TAG, "Url: "+YOUTUBE_VIDEO_INFORMATION_URL);
HttpClient lClient = new DefaultHttpClient();
HttpGet lGetMethod = new HttpGet(YOUTUBE_VIDEO_INFORMATION_URL);
// setting the header as to get the high quality video url
lGetMethod.setHeader("User-Agent", USER_AGENT);
HttpResponse lResp = lClient.execute(lGetMethod);
if(lResp == null)
return null;
String lInfoStr = EntityUtils.toString(lResp.getEntity(), "UTF-8");
Log.e(TAG, "Response: "+lInfoStr);
JSONObject videoInfoJson = new JSONObject(lInfoStr.substring(4,lInfoStr.length()));
JSONObject content = (JSONObject) videoInfoJson.get("content");
JSONObject video = (JSONObject) content.get("video");
JSONArray fmt_stream_map = video.getJSONArray("fmt_stream_map");
url = fmt_stream_map.getJSONObject(0).getString("url");
} catch(Exception e) {
Log.e(TAG, e);
}
Log.e(TAG, "Youtube stream url: "+url);
return url;
}
您应该使用youtubeandroidapi为Android播放YouTube视频,这是最简单的。
这是示例链接