我想在加载网页后3秒后开始播放视频。这是我的html代码:
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="refresh" content="3;url=http://xx.xxx.x.xxx:8080/small.mp4"/>
</head>
<body>
<ul>
<li><a title="Home" alt="Home" href="/">Home</a></li>
<li><a title="Match Centre" alt="Match Centre" href="/menu/centre">Match Centre</a></li>
<li><a title="Clubs" class="more clubs" href="/menu/clubs"><span>Clubs</span></a></li>
<li><a title="Menu" class="more menu" href="/menu/options"><span>Menu</span></a></li>
</ul>
</body>
</html>
它在我尝试过的所有浏览器中都能很好地工作(包括Android noraml浏览器),但它不适用于Android嵌入式浏览器。
这是我的主要活动:
public class MainActivity extends Activity {
private WebView webView;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = (WebView) findViewById(R.id.webView1);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient(){
@Override
public void onReceivedError (WebView view, int errorCode, String description, String failingUrl) {
Log.e("CEmbeddedBrowser eceivedError()", "Fail to load: " + failingUrl);
}
});
webView.setWebChromeClient(new WebChromeClient() {
@Override
public boolean onJsAlert(WebView view, String url, String message, final JsResult result) {
new AlertDialog.Builder(view.getContext())
.setMessage(message)
.setPositiveButton(android.R.string.ok, new AlertDialog.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
result.confirm();
}
})
.setCancelable(false)
.create()
.show();
return true;
}
});
webView.loadUrl("http://xx.xxx.x.xxx:8080/mypage.html");
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Android文档称WebViewClient支持元标记。我已尝试使用普通页面重定向(http://www.google.com">),但我不知道为什么它不适用于视频。
对于像我一样寻找解决方案的其他人来说,我必须指定WebViewClient作为导航目标。元刷新/重定向似乎被视为导航活动,所以当我告诉webview在自己内部处理导航时,它就完美地工作了。
谷歌开发者文档:
处理页面导航
当用户在WebView中单击网页中的链接时,Android的默认行为是启动处理URL的应用程序。通常,默认的web浏览器会打开并加载目标URL。但是,您可以覆盖WebView的此行为,以便在WebView中打开链接。然后,您可以允许用户在WebView维护的网页历史记录中前后导航。
要打开用户单击的链接,只需使用setWebViewClient()为您的WebView提供一个WebViewClient。例如:
WebView myWebView = (WebView) findViewById(R.id.webview); myWebView.setWebViewClient(new WebViewClient());