我只想在WebView或VideoView中播放youtube网址。假设this。我已经遵循了一些教程。但我无法找到方法。
WebViewActivity
在onCreate()
webView = (WebView) findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginState(PluginState.ON);
// webView.getSettings().setPluginsEnabled(true);
final Activity activity = this;
webView.setWebChromeClient(new WebChromeClient()
{
public void onProgressChanged(WebView view , int progress)
{
// Activities and WebViews measure progress with different
// scales.
// The progress meter will automatically disappear when we reach
// 100%
activity.setProgress(progress * 1000);
}
});
webView.setWebViewClient(new MyOwnWebViewClient());
webView.loadData(playVideo, "text/html", "utf-8");
//webView.loadUrl("http://www.androidbegin.com/tutorial/AndroidCommercial.3gp");
//webView.loadUrl("file:///android_asset/Videos/" + extra + ".htm");
webView.setWebViewClient(new MyOwnWebViewClient());
MyOwnWebViewClient是
public class MyOwnWebViewClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view , String url)
{
view.loadUrl(url);
return true;
}
}
VideoActivity
在onCreate()
new VideoViewTask(VideoActivity.this, videoView).execute();
VideoViewTask.java
的位置public class VideoViewTask extends AsyncTask<String, Void, String>
{
ProgressDialog progressDialog;
Context contexto;
VideoView videoViewo;
public VideoViewTask(Context context,VideoView videoView)
{
contexto = context;
videoViewo = videoView;
}
@Override
protected void onPreExecute()
{
super.onPreExecute();
progressDialog = ProgressDialog.show(contexto, "", "Loading Video wait...", true);
}
@Override
protected String doInBackground(String... arg0)
{
String videoUrl = "";
try
{
String url = "http://www.androidbegin.com/tutorial/AndroidCommercial.3gp";//"http://www.youtube.com/watch?v=1FJHYqE0RDg";
videoUrl = getUrlVideoRTSP(url);
//Log.e("Video url for playing=========>>>>>", videoUrl);
}
catch (Exception e)
{
//Log.e("Login Soap Calling in Exception", e.toString());
}
return videoUrl;
}
@Override
protected void onPostExecute(String result)
{
super.onPostExecute(result);
progressDialog.dismiss();
videoViewo.setVideoURI(Uri.parse(result));
MediaController mc = new MediaController(contexto);
videoViewo.setMediaController(mc);
videoViewo.requestFocus();
videoViewo.start();
mc.show();
}
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());
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;
}
}
我只想在我的应用中播放youtube网址。我也通过调用watchYoutubeVideo("oKiYuIsPxYk");
其中
public void watchYoutubeVideo(String id)
{
try
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse("vnd.youtube:" + id));
startActivity(intent);
}
catch (ActivityNotFoundException ex)
{
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse("http://www.youtube.com/watch?v=" + id));
startActivity(intent);
}
}
答案 0 :(得分:0)
请按照以下示例进行操作。我试过Android开发者视频。工作正常。只在应用程序中播放。
WebView wv=(WebView)findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);
//wv.getSettings().setPluginsEnabled(true);
final String mimeType = "text/html";
final String encoding = "UTF-8";
String html = getHTML();
wv.setWebChromeClient(new WebChromeClient() {
});
wv.loadDataWithBaseURL("", html, mimeType, encoding, "");
在oncreate中添加上面的代码
public String getHTML() {
String html = "<iframe class=\"youtube-player\" style=\"border: 0; width: 100%; height: 95%; padding:0px; margin:0px\" id=\"ytplayer\" type=\"text/html\" src=\"http://www.youtube.com/embed/"
+ "s-4J7cijPAo"
+ "?fs=0\" frameborder=\"0\">\n"
+ "</iframe>\n";
return html;
像上面一样插入你的YouTube视频ID。