上次鄙人做了一个简单的利用webView实现的一个浏览器!其中遇到了两个问题,一个是将浏览器中需要下载的内容托管到系统默认的下载程序进行下载,这个比较简单就不在这里讨论了;另一个问题就是我们的Android设备版本是4.0.3,不能像Android2.3那样支持全屏播放视频,这个问题比较纠结,但是经过不断的摸索,终于解决了这个问题。在这里和大家分享一下解决方法:

1、首先定义一个VideoEnabledWebView继承自WebView,复写其中的loadData,loadDataWithBaseURL,loadUrl方法,道理很简单就是在加载url或者js的时候初始化一些内容。见代码:

  1. package com.danielme.android.webviewdemo;
  2. import java.util.Map;
  3. import android.annotation.SuppressLint;
  4. import android.content.Context;
  5. import android.os.Handler;
  6. import android.os.Looper;
  7. import android.util.AttributeSet;
  8. import android.webkit.WebChromeClient;
  9. import android.webkit.WebView;
  10. public class VideoEnabledWebView extends WebView
  11. {
  12. public interface ToggledFullscreenCallback
  13. {
  14. public void toggledFullscreen(boolean fullscreen);
  15. }
  16. private VideoEnabledWebChromeClient videoEnabledWebChromeClient;
  17. private boolean addedJavascriptInterface;
  18. public VideoEnabledWebView(Context context)
  19. {
  20. super(context);
  21. addedJavascriptInterface = false;
  22. }
  23. public VideoEnabledWebView(Context context, AttributeSet attrs)
  24. {
  25. super(context, attrs);
  26. addedJavascriptInterface = false;
  27. }
  28. public VideoEnabledWebView(Context context, AttributeSet attrs, int defStyle)
  29. {
  30. super(context, attrs, defStyle);
  31. addedJavascriptInterface = false;
  32. }
  33. /**
  34. * Pass only a VideoEnabledWebChromeClient instance.
  35. */
  36. @Override
  37. @SuppressLint ("SetJavaScriptEnabled")
  38. public void setWebChromeClient(WebChromeClient client)
  39. {
  40. getSettings().setJavaScriptEnabled(true);
  41. if (client instanceof VideoEnabledWebChromeClient)
  42. {
  43. this.videoEnabledWebChromeClient = (VideoEnabledWebChromeClient) client;
  44. }
  45. super.setWebChromeClient(client);
  46. }
  47. @Override
  48. public void loadData(String data, String mimeType, String encoding)
  49. {
  50. addJavascriptInterface();
  51. super.loadData(data, mimeType, encoding);
  52. }
  53. @Override
  54. public void loadDataWithBaseURL(String baseUrl, String data,
  55. String mimeType, String encoding,
  56. String historyUrl)
  57. {
  58. addJavascriptInterface();
  59. super.loadDataWithBaseURL(baseUrl, data, mimeType, encoding, historyUrl);
  60. }
  61. @Override
  62. public void loadUrl(String url)
  63. {
  64. addJavascriptInterface();
  65. super.loadUrl(url);
  66. }
  67. @Override
  68. public void loadUrl(String url, Map<String, String> additionalHttpHeaders)
  69. {
  70. addJavascriptInterface();
  71. super.loadUrl(url, additionalHttpHeaders);
  72. }
  73. private void addJavascriptInterface()
  74. {
  75. System.out.println(addedJavascriptInterface);
  76. if (!addedJavascriptInterface)
  77. {
  78. // Add javascript interface to be called when the video ends (must be done before page load)
  79. addJavascriptInterface(new Object()
  80. {
  81. }, "_VideoEnabledWebView"); // Must match Javascript interface name of VideoEnabledWebChromeClient
  82. addedJavascriptInterface = true;
  83. }
  84. }
  85. }

其中addJavascriptInterface方法是将一个当前的java对象绑定到一个javascript上面,使用如下方法

webv.addJavascriptInterface(this, "_VideoEnabledWebView");//this为当前对象,绑定到js的_VideoEnabledWebView上面,主要_VideoEnabledWebView的作用域是全局的。这个部分的内容我不是很懂,提供链接给大家学习下,希望看懂的朋友能教教这个步骤是干嘛的!(http://www.oschina.net/code/snippet_232612_8531)

2、定义一个类VideoEnabledWebChromeClient继承自WebChromeClient,这个WebChromeClient中的onShowCustomView方法就是播放网络视频时会被调用的方法,onHideCustomView方法就是视频播放完成会被调用的。其中有个构造函数需要提出来:

  1. public VideoEnabledWebChromeClient(View activityNonVideoView, ViewGroup activityVideoView, View loadingView, VideoEnabledWebView webView)
  2. {
  3. this.activityNonVideoView = activityNonVideoView;
  4. this.activityVideoView = activityVideoView;
  5. this.loadingView = loadingView;
  6. this.webView = webView;
  7. this.isVideoFullscreen = false;
  8. }

这个构造函数中的参数,第一个是webView的父布局,activityVideoView是另外的一个占满整个屏幕的布局,loadingView是播放器的那个显示缓冲状态的view,webView就是webView啦!

见activity_main.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. xmlns:tools="http://schemas.android.com/tools"
  4. android:layout_width="match_parent"
  5. android:layout_height="match_parent"
  6. tools:context=".MainActivity" >
  7. <RelativeLayout
  8. android:id="@+id/nonVideoLayout"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent" >
  11. <com.danielme.android.webviewdemo.VideoEnabledWebView
  12. android:id="@+id/webView"
  13. android:layout_width="match_parent"
  14. android:layout_height="match_parent" />
  15. </RelativeLayout>
  16. <FrameLayout
  17. android:id="@+id/videoLayout"
  18. android:layout_width="match_parent"
  19. android:layout_height="match_parent" >
  20. </FrameLayout>
  21. </RelativeLayout>

不多说了,直接贴代码VideoEnabledWebChromeClient.java代码。

  1. package com.danielme.android.webviewdemo;
  2. import android.app.ActionBar.LayoutParams;
  3. import android.media.MediaPlayer;
  4. import android.media.MediaPlayer.OnCompletionListener;
  5. import android.media.MediaPlayer.OnErrorListener;
  6. import android.media.MediaPlayer.OnPreparedListener;
  7. import android.view.View;
  8. import android.view.ViewGroup;
  9. import android.webkit.WebChromeClient;
  10. import android.widget.FrameLayout;
  11. import android.widget.VideoView;
  12. public class VideoEnabledWebChromeClient extends WebChromeClient implements OnPreparedListener, OnCompletionListener, OnErrorListener
  13. {
  14. public interface ToggledFullscreenCallback
  15. {
  16. public void toggledFullscreen(boolean fullscreen);
  17. }
  18. private View activityNonVideoView;
  19. private ViewGroup activityVideoView;
  20. private View loadingView;
  21. private VideoEnabledWebView webView;
  22. private boolean isVideoFullscreen; // Indicates if the video is being displayed using a custom view (typically full-screen)
  23. private FrameLayout videoViewContainer;
  24. private CustomViewCallback videoViewCallback;
  25. private ToggledFullscreenCallback toggledFullscreenCallback;
  26. /**
  27. * Never use this constructor alone.
  28. * This constructor allows this class to be defined as an inline inner class in which the user can override methods
  29. */
  30. public VideoEnabledWebChromeClient()
  31. {
  32. }
  33. /**
  34. * Builds a video enabled WebChromeClient.
  35. * @param activityNonVideoView A View in the activity's layout that contains every other view that should be hidden when the video goes full-screen.
  36. * @param activityVideoView A ViewGroup in the activity's layout that will display the video. Typically you would like this to fill the whole layout.
  37. */
  38. public VideoEnabledWebChromeClient(View activityNonVideoView, ViewGroup activityVideoView)
  39. {
  40. this.activityNonVideoView = activityNonVideoView;
  41. this.activityVideoView = activityVideoView;
  42. this.loadingView = null;
  43. this.webView = null;
  44. this.isVideoFullscreen = false;
  45. }
  46. /**
  47. * Builds a video enabled WebChromeClient.
  48. * @param activityNonVideoView A View in the activity's layout that contains every other view that should be hidden when the video goes full-screen.
  49. * @param activityVideoView A ViewGroup in the activity's layout that will display the video. Typically you would like this to fill the whole layout.
  50. * @param loadingView A View to be shown while the video is loading (typically only used in API level <11). Must be already inflated and without a parent view.
  51. */
  52. public VideoEnabledWebChromeClient(View activityNonVideoView, ViewGroup activityVideoView, View loadingView)
  53. {
  54. this.activityNonVideoView = activityNonVideoView;
  55. this.activityVideoView = activityVideoView;
  56. this.loadingView = loadingView;
  57. this.webView = null;
  58. this.isVideoFullscreen = false;
  59. }
  60. /**
  61. * Builds a video enabled WebChromeClient.
  62. * @param activityNonVideoView A View in the activity's layout that contains every other view that should be hidden when the video goes full-screen.
  63. * @param activityVideoView A ViewGroup in the activity's layout that will display the video. Typically you would like this to fill the whole layout.
  64. * @param loadingView A View to be shown while the video is loading (typically only used in API level <11). Must be already inflated and without a parent view.
  65. * @param webView The owner VideoEnabledWebView. Passing it will enable the VideoEnabledWebChromeClient to detect the HTML5 video ended event and exit full-screen.
  66. * Note: The web page must only contain one video tag in order for the HTML5 video ended event to work. This could be improved if needed (see Javascript code).
  67. */
  68. public VideoEnabledWebChromeClient(View activityNonVideoView, ViewGroup activityVideoView, View loadingView, VideoEnabledWebView webView)
  69. {
  70. this.activityNonVideoView = activityNonVideoView;
  71. this.activityVideoView = activityVideoView;
  72. this.loadingView = loadingView;
  73. this.webView = webView;
  74. this.isVideoFullscreen = false;
  75. }
  76. /**
  77. * Indicates if the video is being displayed using a custom view (typically full-screen)
  78. * @return true it the video is being displayed using a custom view (typically full-screen)
  79. */
  80. public boolean isVideoFullscreen()
  81. {
  82. return isVideoFullscreen;
  83. }
  84. /**
  85. * Set a callback that will be fired when the video starts or finishes displaying using a custom view (typically full-screen)
  86. * @param callback A VideoEnabledWebChromeClient.ToggledFullscreenCallback callback
  87. */
  88. public void setOnToggledFullscreen(ToggledFullscreenCallback callback)
  89. {
  90. this.toggledFullscreenCallback = callback;
  91. }
  92. @Override
  93. public void onShowCustomView(View view, CustomViewCallback callback)
  94. {
  95. if (view instanceof FrameLayout)
  96. {
  97. // A video wants to be shown
  98. FrameLayout frameLayout = (FrameLayout) view;
  99. View focusedChild = frameLayout.getFocusedChild();
  100. // Save video related variables
  101. this.isVideoFullscreen = true;
  102. this.videoViewContainer = frameLayout;
  103. this.videoViewCallback = callback;
  104. // Hide the non-video view, add the video view, and show it
  105. activityNonVideoView.setVisibility(View.GONE);
  106. activityVideoView.addView(videoViewContainer, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
  107. activityVideoView.setVisibility(View.VISIBLE);
  108. if (focusedChild instanceof VideoView)
  109. {
  110. // VideoView (typically API level <11)
  111. VideoView videoView = (VideoView) focusedChild;
  112. // Handle all the required events
  113. videoView.setOnPreparedListener(this);
  114. videoView.setOnCompletionListener(this);
  115. videoView.setOnErrorListener(this);
  116. }
  117. else // Usually android.webkit.HTML5VideoFullScreen$VideoSurfaceView, sometimes android.webkit.HTML5VideoFullScreen$VideoTextureView
  118. {
  119. // HTML5VideoFullScreen (typically API level 11+)
  120. // Handle HTML5 video ended event
  121. if (webView != null && webView.getSettings().getJavaScriptEnabled())
  122. {
  123. // Run javascript code that detects the video end and notifies the interface
  124. String js = "javascript:";
  125. js += "_ytrp_html5_video = document.getElementsByTagName('video')[0];";
  126. js += "if (_ytrp_html5_video !== undefined) {";
  127. {
  128. js += "function _ytrp_html5_video_ended() {";
  129. {
  130. js += "_ytrp_html5_video.removeEventListener('ended', _ytrp_html5_video_ended);";
  131. js += "_VideoEnabledWebView.notifyVideoEnd();"; // Must match Javascript interface name and method of VideoEnableWebView
  132. }
  133. js += "}";
  134. js += "_ytrp_html5_video.addEventListener('ended', _ytrp_html5_video_ended);";
  135. }
  136. js += "}";
  137. webView.loadUrl(js);
  138. }
  139. }
  140. // Notify full-screen change
  141. if (toggledFullscreenCallback != null)
  142. {
  143. toggledFullscreenCallback.toggledFullscreen(true);
  144. }
  145. }
  146. }
  147. @Override
  148. public void onShowCustomView(View view, int requestedOrientation, CustomViewCallback callback) // Only available in API level 14+
  149. {
  150. onShowCustomView(view, callback);
  151. }
  152. @Override
  153. public void onHideCustomView()
  154. {
  155. // This method must be manually (internally) called on video end in the case of VideoView (typically API level <11)
  156. // This method must be manually (internally) called on video end in the case of HTML5VideoFullScreen (typically API level 11+) because it's not always called automatically
  157. // This method must be manually (internally) called on back key press (from this class' onBackPressed() method)
  158. if (isVideoFullscreen)
  159. {
  160. // Hide the video view, remove it, and show the non-video view
  161. activityVideoView.setVisibility(View.GONE);//播放视频的
  162. activityVideoView.removeView(videoViewContainer);
  163. activityNonVideoView.setVisibility(View.VISIBLE);
  164. // Call back
  165. if (videoViewCallback != null) videoViewCallback.onCustomViewHidden();
  166. // Reset video related variables
  167. isVideoFullscreen = false;
  168. videoViewContainer = null;
  169. videoViewCallback = null;
  170. // Notify full-screen change
  171. if (toggledFullscreenCallback != null)
  172. {
  173. toggledFullscreenCallback.toggledFullscreen(false);
  174. }
  175. }
  176. }
  177. @Override
  178. public View getVideoLoadingProgressView() // Video will start loading, only called in the case of VideoView (typically API level <11)
  179. {
  180. if (loadingView != null)
  181. {
  182. loadingView.setVisibility(View.VISIBLE);
  183. return loadingView;
  184. }
  185. else
  186. {
  187. return super.getVideoLoadingProgressView();
  188. }
  189. }
  190. @Override
  191. public void onPrepared(MediaPlayer mp) // Video will start playing, only called in the case of VideoView (typically API level <11)
  192. {
  193. if (loadingView != null)
  194. {
  195. loadingView.setVisibility(View.GONE);
  196. }
  197. }
  198. @Override
  199. public void onCompletion(MediaPlayer mp) // Video finished playing, only called in the case of VideoView (typically API level <11)
  200. {
  201. onHideCustomView();
  202. }
  203. @Override
  204. public boolean onError(MediaPlayer mp, int what, int extra) // Error while playing video, only called in the case of VideoView (typically API level <11)
  205. {
  206. return false; // By returning false, onCompletion() will be called
  207. }
  208. /**
  209. * Notifies the class that the back key has been pressed by the user.
  210. * This must be called from the Activity's onBackPressed(), and if it returns false, the activity itself should handle it. Otherwise don't do anything.
  211. * @return Returns true if the event was handled, and false if it is not (video view is not visible)
  212. */
  213. public boolean onBackPressed()
  214. {
  215. if (isVideoFullscreen)
  216. {
  217. onHideCustomView();
  218. return true;
  219. }
  220. else
  221. {
  222. return false;
  223. }
  224. }
  225. }

主要是onShowCustomView方法中,当这个方法被调用,将含有webView的那个父布局隐藏掉(GONE),然后将第一个参数view加到布局中。获取第一个参数view的子控件childView,进行判断childView是否属于VideoView(Android 4.0之前是VideoView),如果是Android 4.0之后,则会执行else中的代码,新建String类型js代码,然后调用loadUrl(js)就可以进行视频播放了。其中我个人不知道它是如何通过js来播放视频的,我觉得和之前的addJavascriptInterface这个方法有一定关系,希望知道如何实现的能够指导一下本人。其它的函数就很好理解了。

其中多说一句,Android 4.0之前的那个第一个参数view是videoView,Android 4.0之后是那个HTML5VideoFullScreen$VideoSurfaceView

android 4.0以上WebView不能全屏播放视频的解决办法相关推荐

  1. QT视频客户端全屏后视频卡住解决办法

    QT编写视频监控客户端全屏后会发生视频卡住的问题,该问题的解决办法是重载showEvent事件,按照如下方式实现. void VideoCanvas::showEvent(QShowEvent * e ...

  2. Android:Android9.0使用 AndroidVideoCache时不能缓存播放视频的解决

    一.问题现象: 项目中使用 https://github.com/danikula/AndroidVideoCache 作为视频缓存组件,但是在9.0手机上无法正常缓存,并且报错: 1.详细错误截图 ...

  3. Android全屏播放视频~包括刘海屏、隐藏时间状态栏

    需求是全屏播放视频,刘海屏上面也要播放. 下面是我实现的方式: 首先创建 CustomVideoView 工具类: import android.annotation.TargetApi; impor ...

  4. 记一次微信H5全屏播放视频的总结

    一.H5场景介绍 需求:在微信里打开一个H5页面,然后点击按钮全屏播放视频,等视频播放完成后,在视频上显示一个跳转按钮,点击按钮跳转到其他的页面. 二.遇到的问题 1.IOS设备微信上,视频不能预加载 ...

  5. 微信内置浏览器 非全屏播放视频解析

    前提条件,接了一个项目要实现在微信公众号里课程播放,而且还有评论功能,视频需要小窗播放. 首先公布解决方案: 感谢知乎上的回答,原版微信内置浏览器 如何小窗不全屏播放视频? 感谢该问题的徐霖同学的回答 ...

  6. [RK3399][Android7.1] 调试笔记 --- 闪电浏览器全屏播放视频时黑屏

    Platform: RK3399 OS: Android 7.1 Kernel: v4.4.83 现象: 使用默认闪电浏览器全屏播放视频时黑屏, error log如下: 08-09 17:19:45 ...

  7. []转载]微信内置浏览器 非全屏播放视频解析

    前提条件,接了一个项目要实现在微信公众号里课程播放,而且还有评论功能,视频需要小窗播放.首先公布解决方案: 感谢知乎上的回答,原版[微信内置浏览器 如何小窗不全屏播放视频?]感谢该问题的徐霖同学的回答 ...

  8. 微信内置浏览器 非全屏播放视频解析 1

    前提条件,接了一个项目要实现在微信公众号里课程播放,而且还有评论功能,视频需要小窗播放. 首先公布解决方案: 感谢知乎上的回答,原版[ 微信内置浏览器 如何小窗不全屏播放视频?] 感谢该问题的徐霖同学 ...

  9. uniapp 判断页面是否是横竖屏,解决微信小程序video组件全屏播放视频遮盖自定义播放控件问题

    如果res.deviceOrientation 等于landscape 的话是竖屏,portrait则是横屏.因为用户每旋转一次屏幕就会触发里面的onShow钩子,因此在页面显示或横竖屏变化都会触发这 ...

最新文章

  1. golang实践LSM相关内容
  2. Android中获取系统内存信息以及进程信息-----ActivityManager的使用(一)
  3. VC++读取图像RGB值
  4. Python自动化运维——系统性能信息模块
  5. java中CyclicBarrier的使用
  6. latex正文显示运算符
  7. HTML map元素
  8. 控制使用期限_学校厨房设备延长其使用寿命的方法有哪些呢?
  9. freemarker【FTL】常见语法大全
  10. HDU-1716 排列2 组合数
  11. Elasticsearch 之索引创建原则
  12. C++单例模式(懒汉模式)实现
  13. linux配置dhcp超级作用域,Linux DHCP服务器 超级作用域
  14. 联想用u盘重装系统步骤_联想笔记本u盘重装系统,小编教你联想笔记本怎么使用u盘重装系统...
  15. JQuery温故而知新
  16. HTML背景带视频的个人炫酷引导页源码
  17. GBase 8c基础操作
  18. linux车机按键学习,linux就该这么学
  19. 分享一个时间增加的办法
  20. 同样是IT行业,测试和开发薪资真有这么大差别?

热门文章

  1. 大林算法计算机控制实验报告,大林算法
  2. loop指令 c语言,arm汇编loop指令
  3. Window下VS运行达梦DPI
  4. C++学习笔记-----std::string的=,+,+=对int,char类型操作数的支持
  5. 透视映射和射影映射的关系 Perspective and Projectivity
  6. oracle 增加ora容量_案例:Oracle报错ORA-01144 详解数据文件大小32GB的限制的原因
  7. c++primer练习13.42
  8. html代码中本地路径里斜杠 / 和反斜杠 \ 的区别
  9. ofstream与ate的故事
  10. CentOS 7.6 下安装 MySQL8.0.13