【发布时间】:2014-11-05 01:08:06
【问题描述】:
我正在尝试在原生 Android 应用中播放 YouTube。 这是我的代码,但我得到了无法播放此视频的结果,所以请您帮忙看看该代码有什么问题
public class Youtubeplay extends AsyncTask<Void, Void, Void> {
ProgressDialog progressDialog;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = ProgressDialog.show(MainActivity.this, "",
"Loading Video wait...", true);
}
@Override
protected Void doInBackground(Void... params) {
try {
String url = "https://www.youtube.com/watch?v=ZA7Kkhv30WA";
videoUrl = getUrlVideoRTSP(url);
Log.e("Video url for playing=========>>>>>", videoUrl);
} catch (Exception e) {
Log.e("Login Soap Calling in Exception", e.toString());
}
return null;
}
private String getUrlVideoRTSP(String urlYoutube) {
// TODO Auto-generated method stub
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();
org.w3c.dom.Document doc = documentBuilder.parse(connection
.getInputStream());
Element el = (Element) doc.getDocumentElement();
NodeList list = ((org.w3c.dom.Document) 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 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;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
progressDialog.dismiss();
videoView.setVideoURI(Uri.parse(videoUrl));
MediaController mc = new MediaController(MainActivity.this);
videoView.setMediaController(mc);
videoView.requestFocus();
videoView.start();
mc.show();
}
}
【问题讨论】:
-
欢迎来到 StackOverflow,我不是 android 开发人员,所以无法帮助您解决问题。但是,作为一般提示,您需要在问题中提供更多信息。您已经告诉我们您正在尝试做什么,但是我们需要更多关于出了什么问题的信息。同时发布您的整个班级也非常无益。您需要准确缩小导致问题的范围,并仅发布问题代码。
-
这似乎是重复的问题..作为stackoverflow的新手,我认为您不知道这里的规则..使用适当关键字的google,您可以找到解决方案..人们可能会否决重复的问题..快乐编码:)