I simply want to play youtube url in WebView or VideoView. Suppose this . I have followed number of tutorials.But I am unable to find a way.
WebViewActivity
In 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());
And MyOwnWebViewClient is
public class MyOwnWebViewClient extends WebViewClient
{
@Override
public boolean shouldOverrideUrlLoading(WebView view , String url)
{
view.loadUrl(url);
return true;
}
}
VideoActivity
In onCreate()
new VideoViewTask(VideoActivity.this, videoView).execute();
Where VideoViewTask.java is
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;
}
}
I simply want to play youtube url in my app. I have also try it by calling watchYoutubeVideo("oKiYuIsPxYk");
Where
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);
}
}