How to Stop the Webview video in android
android, android-webview, java
Solution
It's a little hacky, but if you want to have a pause/play of the video when your activity goes off/on, use this in your Activity:
@Override
public void onPause()
{
super.onPause();
toggleWebViewState(true);
}
@Override
public void onResume()
{
super.onResume();
toggleWebViewState(false);
}
private void toggleWebViewState(boolean pause)
{
try
{
Class.forName("android.webkit.WebView")
.getMethod(pause
? "onPause"
: "onResume", (Class[]) null)
.invoke(webview, (Object[]) null);
}
catch (Exception e){}
}
Problem
In my project using webview loading the video url using iframe concept ,but call the next inten cannot stop the playing sound or pause the video. or any other coding for loding the video in webview my code : ``` public class Webviewfullscreen extends Activity { WebView webview; LinearLayout webViewPlaceholder; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.webviewfull); webViewPlaceholder=(LinearLayout) findViewById(R.id.webViewholder); String VideoUrl=this.getIntent().getExtras().getString("url"); webview = (WebView) findViewById(R.id.webView1); webViewDisplay(VideoUrl, webview); } private void webViewDisplay(final String weburl, final WebView www) { new Handler().post(new Runnable() { @SuppressWarnings("deprecation") @Override public void run() { WebSettings settings = www.getSettings(); String Url = "<html> <head> <style type=text/css> iframe {height:100%;width:100%;margin:0;padding:0;overflow:scroll;} body {background-color:#000; margin:0;}</style> </head> <body> <iframe width=240px height=220px src=" + weburl + " frameborder=0 webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe></body></html>"; settings.setLightTouchEnabled(true); www.getSettings().setJavaScriptEnabled(true); if (Build.VERSION.SDK_INT < 8) { www.getSettings().setPluginsEnabled(true); } else { www.getSettings().setPluginState(PluginState.ON); } www.setWebChromeClient(new WebChromeClient()); www.loadDataWithBaseURL(null, Url, "text/html", "utf-8", null); } }); } @Override protected void onDestroy() { // TODO Auto-generated method stub super.onDestroy(); } @Override public boolean onKeyDown(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_BACK && event.getRepeatCount() == 0) { // do something on back. System.out.println("on finish"); webViewPlaceholder.removeView(webview); webview.loadUrl(""); webview.removeAllViews(); webview.destroy(); webview = null; finish(); return true; } return super.onKeyDown(keyCode, event); } } ``` Please help how to stop the video