Android WebView H5视频播放实现全屏播放功能、全屏按钮不显示、灰显、点击无效问题解决方案
•
移动开发
Android WebView H5视频播放实现全屏播放功能、全屏按钮不显示、灰显、点击无效问题解决方案
-
- 一、官方介绍
- 二、实现解决
- 三、写在最后
一、官方介绍
HTML5 video support HTML5 Video support In order to support inline HTML5 video in your application, you need to have hardware acceleration turned on, and set a WebChromeClient. For full screen support, implementations of onShowCustomView(View, WebChromeClient.CustomViewCallback) and onHideCustomView() are required, getVideoLoadingProgressView() is optional.
- 打开硬件加速(3.0以上版本支持)
- set一个WebChromClient,实现onShowCustomView() 方法和onHideCustomView()方法
- 全屏支持
二、实现解决
- 打开硬件加速
- 在Manifest中,对应的Activity添加: android:hardwareAccelerated = “true”。
- 防止h5重新加载:Manifest中,对应的Activity添加: android:configChanges=“keyboardHidden|orientation|screenSize”
- 切换横屏时,屏幕的H5内容始终以竖屏显示:Manifest中,对应的Activity添加: android:screenOrientation=“portrait”
-
WebView中设置WebChromClient实现接口onShowCustomView() 方法和onHideCustomView()方法, 实现后即显示全屏播放按钮,但是点击无反应,需要实现全屏支持。
-
全屏支持实现:WebView在点击全屏按钮后调用onShowCustomView方法,而全屏的视频会在其参数view中进行渲染。我们需要在Activity中写一个viewRoot,在onShowCustomView触发后,将其view传入viewRoot,且使APP横屏,达到全屏显示。
@SuppressLint("StaticFieldLeak")
private var mCustomView: View? = null //全屏渲染视频的View
private var webRoot: FrameLayout? = null// 显示全屏视频的布局
private var mCustomViewCallback: CustomViewCallback? = null
@SuppressLint("SourceLockedOrientationActivity")
override fun onShowCustomView(view: View?, callback: CustomViewCallback?) {
super.onShowCustomView(view, callback)
try {
if (mCustomView != null) { //当上一个view存在时,隐藏全屏
callback?.onCustomViewHidden()
return
}
mCustomView = view
webRoot?.addView(mCustomView)
mCustomViewCallback = callback
webView?.visibility = View.GONE
val actionBar = supportActionBar
actionBar?.hide()
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
} catch (e: Exception) {
e.printStackTrace()
}
}
@SuppressLint("SourceLockedOrientationActivity")
override fun onHideCustomView() {
try {
webView?.visibility = View.VISIBLE
val actionBar = supportActionBar
actionBar?.show()
if (mCustomView == null) { //当上一个view不存在时,不处理
return
}
mCustomView?.visibility = View.GONE
webRoot?.removeView(mCustomView)
mCustomViewCallback?.onCustomViewHidden()
mCustomView = null
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
} catch (e: Exception) {
e.printStackTrace()
}
super.onHideCustomView()
}
override fun onProgressChanged(view: WebView?, newProgress: Int) {
super.onProgressChanged(view, newProgress)
progressBar?.progress = newProgress
}
xml
三、写在最后
此文章为个人开发时记录,有时时间有限,无法深入研究,若看到此文章后有其他见解或解决方式,欢迎留言交流👇👇👇
————————————————
版权声明:转载请附上原文出处链接及本声明。
原文链接:
https://blog.csdn.net/weixin_44158429/article/details/130217214
本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://www.net2asp.com/9ef616ce9c.html
