当前位置:   article > 正文

WebView使用详解、H5网页视频全屏播放 、网页跳转空白_webview播放视频

webview播放视频

转载请注明出处:WebView使用详解、H5网页视频全屏播放 、网页跳转空白_fragment webview全屏播放视频_Mr_Leixiansheng的博客-CSDN博客

内容:介绍webview的使用方法,介绍WebViewClient、WebChromeClient,H5网页视频全屏播放,网页跳转空白问题

最近做项目老爱和H5打交道,遇到了很多问题也踩了许多坑,今天在这儿总结下,方便后人乘凉。

关于安卓和H5交互可参考我之前的文章:原生与H5交互介绍

WebView基础设置

  1. private void initWebView() {
  2. mWebView.setWebViewClient(new MyWebViewClient()); //设置在WebView中打开链接,不设置则调用自带浏览器。主要针对View进行拦截处理
  3. mWebView.setWebChromeClient(new MyWebChromeClient());
  4. WebSettings webSettings = mWebView.getSettings();
  5. webSettings.setJavaScriptEnabled(true); //支持JS
  6. webSettings.setDomStorageEnabled(true); //启用dom内存,防止js加载失败
  7. webSettings.setAllowFileAccess(true); //允许访问文件
  8. webSettings.setSupportZoom(true); //支持缩放
  9. webSettings.setLoadWithOverviewMode(true); //是否启动概述模式浏览界面,当页面宽度超过WebView显示宽度时,缩小页面适应WebView。默认false
  10. webSettings.setGeolocationEnabled(false); //是否允许定位
  11. webSettings.setLoadsImagesAutomatically(true); //是否加载图片
  12. // webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); //设置缓存模式
  13. // webSettings.setDefaultTextEncodingName("UTF-8"); //设置页面的编码格式,默认UTF-8
  14. }

WebViewClient主要是对view一系列操作进行监听拦截。包括:网页加载开始、网页加载完成、错误拦截处理。(代码中注释会详解,再次不多做介绍)

WebChromeClient主要是对浏览器进行监听。例如弹窗、是否显示支持全屏播放等。(代码中注释会详解,再次不多做介绍)

网页加载空白问题,我只在7.0及以上遇到,貌似是说证书错误,可能越往上安全性越高吧,按照下面处理下就行了

  1. /**
  2. * HTTPS通信的网址(以https://开头的网站)出现错误时
  3. * 证书错误拦截处理
  4. * 安卓7.0需要
  5. */
  6. @Override
  7. public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
  8. if(error.getPrimaryError() == android.net.http.SslError.SSL_INVALID ){// 校验过程遇到了bug
  9. handler.proceed(); //忽略错误继续加载
  10. }else{
  11. handler.cancel(); //取消加载
  12. }
  13. }

视频全屏播放:安卓不像IOS一样可以直接全屏播放,需要在WebChromeClient对其进行设置,相当于是new 一个Fragment让其来进行全屏播放

  1. /*** 视频播放相关的方法 **/
  2. @Override
  3. public View getVideoLoadingProgressView() {
  4. FrameLayout frameLayout = new FrameLayout(MainActivity.this);
  5. frameLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
  6. return frameLayout;
  7. }
  8. @Override
  9. public void onShowCustomView(View view, CustomViewCallback callback) {
  10. showCustomView(view, callback);
  11. }
  12. @Override
  13. public void onHideCustomView() {
  14. hideCustomView();
  15. }

   

代码如下:

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:app="http://schemas.android.com/apk/res-auto"
  4. xmlns:tools="http://schemas.android.com/tools"
  5. android:layout_width="match_parent"
  6. android:layout_height="match_parent"
  7. android:orientation="vertical">
  8. <Button
  9. android:id="@+id/btn_load"
  10. android:layout_width="match_parent"
  11. android:layout_height="wrap_content"
  12. android:text="LOAD"/>
  13. <RelativeLayout
  14. android:layout_width="match_parent"
  15. android:layout_height="match_parent">
  16. <WebView
  17. android:id="@+id/web_view"
  18. android:layout_width="match_parent"
  19. android:layout_height="match_parent">
  20. </WebView>
  21. <ProgressBar
  22. android:id="@+id/pb_loading"
  23. android:layout_width="wrap_content"
  24. android:layout_height="wrap_content"
  25. android:layout_centerInParent="true"
  26. android:visibility="gone"/>
  27. </RelativeLayout>
  28. </LinearLayout>
  1. package com.example.leixiansheng.webviewtest;
  2. import android.Manifest;
  3. import android.content.Context;
  4. import android.content.Intent;
  5. import android.graphics.Bitmap;
  6. import android.net.Uri;
  7. import android.net.http.SslError;
  8. import android.os.Build;
  9. import android.os.Bundle;
  10. import android.support.annotation.NonNull;
  11. import android.support.v4.app.ActivityCompat;
  12. import android.support.v7.app.AppCompatActivity;
  13. import android.text.TextUtils;
  14. import android.util.Log;
  15. import android.view.KeyEvent;
  16. import android.view.MotionEvent;
  17. import android.view.View;
  18. import android.view.ViewGroup;
  19. import android.view.WindowManager;
  20. import android.webkit.JsPromptResult;
  21. import android.webkit.JsResult;
  22. import android.webkit.SslErrorHandler;
  23. import android.webkit.ValueCallback;
  24. import android.webkit.WebChromeClient;
  25. import android.webkit.WebResourceError;
  26. import android.webkit.WebResourceRequest;
  27. import android.webkit.WebSettings;
  28. import android.webkit.WebView;
  29. import android.webkit.WebViewClient;
  30. import android.widget.Button;
  31. import android.widget.FrameLayout;
  32. import android.widget.ProgressBar;
  33. import com.tbruyelle.rxpermissions2.Permission;
  34. import com.tbruyelle.rxpermissions2.RxPermissions;
  35. import java.util.function.Consumer;
  36. import butterknife.BindView;
  37. import butterknife.ButterKnife;
  38. import butterknife.OnClick;
  39. public class MainActivity extends AppCompatActivity {
  40. @BindView(R.id.btn_load)
  41. Button mBtnLoad;
  42. @BindView(R.id.web_view)
  43. WebView mWebView;
  44. @BindView(R.id.pb_loading)
  45. ProgressBar mPbLoading;
  46. /** 视频全屏参数 */
  47. protected static final FrameLayout.LayoutParams COVER_SCREEN_PARAMS = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
  48. private View customView;
  49. private FrameLayout fullscreenContainer;
  50. private WebChromeClient.CustomViewCallback customViewCallback;
  51. @Override
  52. protected void onCreate(Bundle savedInstanceState) {
  53. super.onCreate(savedInstanceState);
  54. setContentView(R.layout.activity_main);
  55. ButterKnife.bind(this);
  56. initWebView();
  57. }
  58. private void initWebView() {
  59. mWebView.setWebViewClient(new MyWebViewClient()); //设置在WebView中打开链接,不设置则调用自带浏览器。主要针对View进行拦截处理
  60. mWebView.setWebChromeClient(new MyWebChromeClient());
  61. WebSettings webSettings = mWebView.getSettings();
  62. webSettings.setJavaScriptEnabled(true); //支持JS
  63. webSettings.setDomStorageEnabled(true); //启用dom内存,防止js加载失败
  64. webSettings.setAllowFileAccess(true); //允许访问文件
  65. webSettings.setSupportZoom(true); //支持缩放
  66. webSettings.setLoadWithOverviewMode(true); //是否启动概述模式浏览界面,当页面宽度超过WebView显示宽度时,缩小页面适应WebView。默认false
  67. webSettings.setGeolocationEnabled(false); //是否允许定位
  68. webSettings.setLoadsImagesAutomatically(true); //是否加载图片
  69. // webSettings.setCacheMode(WebSettings.LOAD_DEFAULT); //设置缓存模式
  70. // webSettings.setDefaultTextEncodingName("UTF-8"); //设置页面的编码格式,默认UTF-8
  71. }
  72. @OnClick(R.id.btn_load)
  73. public void onViewClicked() {
  74. mWebView.loadUrl("http://www.baidu.com");
  75. }
  76. @Override
  77. public boolean onKeyDown(int keyCode, KeyEvent event) {
  78. if (keyCode == KeyEvent.KEYCODE_BACK) {
  79. //优先退出全屏播放
  80. if (customView != null) {
  81. hideCustomView();
  82. return true;
  83. } else {
  84. if (mWebView.canGoBack()) {
  85. mWebView.goBack(); //返回上个页面
  86. return true;
  87. } else {
  88. System.exit(0); //退出程序
  89. }
  90. }
  91. }
  92. return super.onKeyDown(keyCode, event);
  93. }
  94. /**
  95. * 针对网页进行拦截处理
  96. */
  97. public class MyWebViewClient extends WebViewClient{
  98. /**
  99. *可以实现对网页中超链接的拦截
  100. */
  101. @Override
  102. public boolean shouldOverrideUrlLoading(WebView view, String url) {
  103. //例:拦截电话网址,直接调用本地电话
  104. if (url.contains("tel:")){
  105. startActivity(new Intent(Intent.ACTION_DIAL, Uri.parse(url)));
  106. return true;
  107. }
  108. if (url.startsWith("http:") || url.startsWith("https:")) {
  109. view.loadUrl(url);
  110. return true;
  111. }
  112. /* WebView.HitTestResult hitTestResult = view.getHitTestResult();
  113. //hitTestResult==null解决重定向问题
  114. if (!TextUtils.isEmpty(url) && hitTestResult == null) {
  115. view.loadUrl(url);
  116. return true;
  117. }*/
  118. return super.shouldOverrideUrlLoading(view, url);
  119. }
  120. /**
  121. *开始加载
  122. */
  123. @Override
  124. public void onPageStarted(WebView view, String url, Bitmap favicon) {
  125. super.onPageStarted(view, url, favicon);
  126. mPbLoading.setVisibility(View.VISIBLE);
  127. }
  128. /**
  129. * 结束加载
  130. */
  131. @Override
  132. public void onPageFinished(WebView view, String url) {
  133. super.onPageFinished(view, url);
  134. mPbLoading.setVisibility(View.GONE);
  135. }
  136. /**
  137. * 加载错误的时候会产生这个回调
  138. */
  139. @Override
  140. public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
  141. super.onReceivedError(view, errorCode, description, failingUrl);
  142. //TODO
  143. }
  144. /**
  145. * HTTPS通信的网址(以https://开头的网站)出现错误时
  146. * 证书错误拦截处理
  147. * 安卓7.0需要
  148. */
  149. @Override
  150. public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
  151. if(error.getPrimaryError() == android.net.http.SslError.SSL_INVALID ){// 校验过程遇到了bug
  152. handler.proceed(); //忽略错误继续加载
  153. }else{
  154. handler.cancel(); //取消加载
  155. }
  156. }
  157. }
  158. /**
  159. * 针对浏览器拦截处理
  160. */
  161. public class MyWebChromeClient extends WebChromeClient{
  162. /**
  163. * 弹窗拦截
  164. */
  165. @Override
  166. public boolean onJsAlert(WebView view, String url, String message, JsResult result) {
  167. return super.onJsAlert(view, url, message, result);
  168. }
  169. /**
  170. * 弹窗拦截
  171. */
  172. @Override
  173. public boolean onJsConfirm(WebView view, String url, String message, JsResult result) {
  174. return super.onJsConfirm(view, url, message, result);
  175. }
  176. /**
  177. * 弹窗拦截
  178. */
  179. @Override
  180. public boolean onJsPrompt(WebView view, String url, String message, String defaultValue, JsPromptResult result) {
  181. return super.onJsPrompt(view, url, message, defaultValue, result);
  182. }
  183. /**
  184. * 加载进度拦截
  185. */
  186. @Override
  187. public void onProgressChanged(WebView view, int newProgress) {
  188. super.onProgressChanged(view, newProgress);
  189. }
  190. /**
  191. * 文件选择
  192. */
  193. @Override
  194. public boolean onShowFileChooser(WebView webView, ValueCallback<Uri[]> filePathCallback, FileChooserParams fileChooserParams) {
  195. selectImage();
  196. return super.onShowFileChooser(webView, filePathCallback, fileChooserParams);
  197. }
  198. /*** 视频播放相关的方法 **/
  199. @Override
  200. public View getVideoLoadingProgressView() {
  201. FrameLayout frameLayout = new FrameLayout(MainActivity.this);
  202. frameLayout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
  203. return frameLayout;
  204. }
  205. @Override
  206. public void onShowCustomView(View view, CustomViewCallback callback) {
  207. showCustomView(view, callback);
  208. }
  209. @Override
  210. public void onHideCustomView() {
  211. hideCustomView();
  212. }
  213. }
  214. /**
  215. * 图片选择
  216. */
  217. private void selectImage() {
  218. //TODO
  219. }
  220. /**
  221. * 视频播放全屏
  222. */
  223. private void showCustomView(View view, WebChromeClient.CustomViewCallback callback) {
  224. // if a view already exists then immediately terminate the new one
  225. if (customView != null) {
  226. callback.onCustomViewHidden();
  227. return;
  228. }
  229. getWindow().getDecorView();
  230. //获取虚拟按键高度,防止遮挡
  231. if(ScreenUtils.hasNavBar(this)){
  232. COVER_SCREEN_PARAMS.setMargins(0,0,0,ScreenUtils.getNavigationBarHeight(this));
  233. }
  234. FrameLayout decor = (FrameLayout) getWindow().getDecorView();
  235. fullscreenContainer = new FullscreenHolder(this);
  236. fullscreenContainer.addView(view, COVER_SCREEN_PARAMS);
  237. decor.addView(fullscreenContainer, COVER_SCREEN_PARAMS);
  238. customView = view;
  239. setStatusBarVisibility(false);
  240. customViewCallback = callback;
  241. }
  242. /**
  243. * 隐藏视频全屏
  244. */
  245. private void hideCustomView() {
  246. if (customView == null) {
  247. return;
  248. }
  249. setStatusBarVisibility(true);
  250. FrameLayout decor = (FrameLayout) getWindow().getDecorView();
  251. decor.removeView(fullscreenContainer);
  252. fullscreenContainer = null;
  253. customView = null;
  254. customViewCallback.onCustomViewHidden();
  255. mWebView.setVisibility(View.VISIBLE);
  256. }
  257. /**
  258. * 全屏容器界面
  259. */
  260. static class FullscreenHolder extends FrameLayout {
  261. public FullscreenHolder(Context ctx) {
  262. super(ctx);
  263. setBackgroundColor(ctx.getResources().getColor(android.R.color.black));
  264. }
  265. @Override
  266. public boolean onTouchEvent(MotionEvent evt) {
  267. return true;
  268. }
  269. }
  270. private void setStatusBarVisibility(boolean visible) {
  271. int flag = visible ? 0 : WindowManager.LayoutParams.FLAG_FULLSCREEN;
  272. getWindow().setFlags(flag, WindowManager.LayoutParams.FLAG_FULLSCREEN);
  273. }
  274. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Cpp五条/article/detail/174890
推荐阅读
相关标签
  

闽ICP备14008679号