在我的项目中使用webview使用iframe概念加载视频网址,但是调用下一个网页无法停止播放声音或暂停视频。或任何其他编码用于在webview中录制视频
我的代码:
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);
}
}
请帮助停止视频
答案 0 :(得分:5)
有点hacky,但是如果你想在活动开始时暂停/播放视频,请在你的活动中使用它:
@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){}
}
答案 1 :(得分:3)
花了很多时间后,我得出结论暂停视频正在播放HTML的WebView iframe概念。
只需覆盖Activity或Fragment上的onPause()方法,您可以使用webView并调用WebView onPause()方法。它对我有用。
@Override
public void onPause() {
super.onPause();
mWebView.onPause();
}
迟到发布这个答案,但这可能有助于其他人。
答案 2 :(得分:0)
In onPause()
method , set webview to a blank url like below.
webview.loadurl("");
and it works for me , just try it.