在日常的android开发中,我们有时会用到 WebView去加载一个html文件,注意有时加载的是整个html文件的内容,而不是加载html文件的路径,于是需要用到webView的方法:
mWebView.loadDataWithBaseURL(null, s, "text/html", "utf-8", null);
/** * Loads the given data into this WebView, using baseUrl as the base URL for * the content. The base URL is used both to resolve relative URLs and when * applying JavaScript's same origin policy. The historyUrl is used for the * history entry. * <p> * Note that content specified in this way can access local device files * (via 'file' scheme URLs) only if baseUrl specifies a scheme other than * 'http', 'https', 'ftp', 'ftps', 'about' or 'javascript'. * <p> * If the base URL uses the data scheme, this method is equivalent to * calling {@link #loadData(String,String,String) loadData()} and the * historyUrl is ignored, and the data will be treated as part of a data: URL. * If the base URL uses any other scheme, then the data will be loaded into * the WebView as a plain string (i.e. not part of a data URL) and any URL-encoded * entities in the string will not be decoded. * <p> * Note that the baseUrl is sent in the 'Referer' HTTP header when * requesting subresources (images, etc.) of the page loaded using this method. * * @param baseUrl the URL to use as the page's base URL. If null defaults to * 'about:blank'. * @param data a String of data in the given encoding * @param mimeType the MIMEType of the data, e.g. 'text/html'. If null, * defaults to 'text/html'. * @param encoding the encoding of the data * @param historyUrl the URL to use as the history entry. If null defaults * to 'about:blank'. If non-null, this must be a valid URL. */ public void loadDataWithBaseURL(String baseUrl, String data, String mimeType, String encoding, String historyUrl) { checkThread(); mProvider.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl); }
首先,你需要确认的是 html 文件中的 Video 是是否能播放,测试的方法是将html的内容复制到html文件中,然后用浏览自加载html文件路径,看在浏览器中文件是否能正常播放。
ok,在第一步确认video正常以后,再来查找webView的加载问题。首先,若加载内容需要联网的,你需要给你的app加上联网权限,然后在你webView界面的activity需要硬件加速的配置,去mainfast 配置文件中,找到显示webview控件 的activity(例如我的activity命名为:OffersInfoActivity),做如下设置:
<activity android:name=".ui.offersInfo.OffersInfoActivity" android:hardwareAccelerated="true"/>
android:hardwareAccelerated="true"
然后去activity界面做 webView 的设置,下面直接贴webview的设置代码:
WebSettings webSettings = mWebView.getSettings(); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { webSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW); } webSettings.setDefaultTextEncodingName("utf-8") ;//这句话去掉也没事。。只是设置了编码格式 webSettings.setJavaScriptEnabled(true); //这句话必须保留。。不解释 webSettings.setDomStorageEnabled(true);//这句话必须保留。。否则无法播放优酷视频网页。。其他的可以 mWebView.setWebChromeClient(new WebChromeClient());//重写一下。有的时候可能会出现问题 mWebView.setWebViewClient(new WebViewClient(){//不写的话自动跳到默认浏览器了。。跳出APP了。。怎么能不写? public boolean shouldOverrideUrlLoading(WebView view, String url) {//这个方法必须重写。否则会出现优酷视频周末无法播放。周一-周五可以播放的问题 if(url.startsWith("intent")||url.startsWith("youku")){ return true; }else{ return super.shouldOverrideUrlLoading(view, url); } } });//最后一步,加载html文件的内容:
mWebView.loadDataWithBaseURL(null, s, "text/html", "utf-8", null);
@Override protected void onDestroy() { if (mWebView != null) { mWebView.destroy(); } super.onDestroy(); }
ok,以上代码亲测可用,今天关于webview无法加载视频的内容就讲到这里,本篇文章的内容参考了以下链接:
http://blog.csdn.net/a872822645/article/details/54708566
在此感谢!