kotlin 实现 webview 渲染视频 支持全屏播放,增加自定义返回键退出全屏
- webview 本身不支持渲染的视频全屏播放,需要自定义实现;
- 重写 WebChromeClient,在onShowCustomView 中增加全屏的视图,然后隐藏webview;
- 取消全屏视图时,需要打开webview;
- 全屏时,视屏不带返回键,需要增加自定义返回键;
- 处理返回键时,即隐藏全屏,打开已定义的webview;
1. 初始化webview,此处用compose 实现
val webView = remember {
WebView(context).apply {
initWebViewSettings(settings)
setBackgroundColor(getColor(R.color.zen_111111))
webViewClient = CustomWebViewClient()
webChromeClient = CustomChromeClient()
if (webUrl.isNotEmpty()) {
loadUrl(webUrl)
}
layoutParams = ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
}
}
2 initWebViewSettings 函数设置
fun initWebViewSettings(settings: WebSettings) {
settings.javaScriptEnabled = true
settings.useWideViewPort = true
settings.loadWithOverviewMode = true
settings.allowFileAccess = true
settings.mixedContentMode = WebSettings.MIXED_CONTENT_ALWAYS_ALLOW
settings.setSupportZoom(true)
settings.javaScriptCanOpenWindowsAutomatically = true
settings.pluginState = WebSettings.PluginState.ON
settings.domStorageEnabled = true
settings.mediaPlaybackRequiresUserGesture = false
//settings.safeBrowsingEnabled = true
settings.cacheMode = WebSettings.LOAD_CACHE_ELSE_NETWORK
}
3 重写 WebChromeClient
class CustomChromeClient :WebChromeClient(){overridefunonProgressChanged(view: WebView?, newProgress: Int){
progress.intValue = newProgress
}overridefunonShowCustomView(view: View, callback: CustomViewCallback){if(container !=null){
callback.onCustomViewHidden()return}
customView = view
customViewCallback = callback
// 添加一个返回键val backButton =ImageButton(mContext)
backButton.setBackgroundColor(Color.TRANSPARENT)// 添加返回按钮
backButton.setImageResource(R.mipmap.xx)
backButton.setOnClickListener{onHideCustomView()}
container =FrameLayout(mContext!!)
container?.addView(customView,
FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT
))
container?.addView(backButton,
FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.START or Gravity.TOP
))(findViewById<FrameLayout>(android.R.id.content)).addView(
container,
FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT
))
webActivityView?.visibility = View.GONE
}overridefunonHideCustomView(){super.onHideCustomView()if(container ==null){return}(findViewById<FrameLayout>(android.R.id.content)).removeView(container)
container =null
webActivityView?.visibility = View.VISIBLE
customViewCallback?.onCustomViewHidden()}}
4 CustomWebViewClient 重写 shouldOverrideUrlLoading
inner class CustomWebViewClient : WebViewClient() {
@SuppressLint("WebViewClientOnReceivedSslError")
override fun onReceivedSslError(
view: WebView?,
handler: SslErrorHandler?,
error: SslError?
) {
Timber.tag(TAG).e("onReceivedSslError %s", error.toString())
handler?.proceed()
}
override fun shouldOverrideUrlLoading(
view: WebView?,
request: WebResourceRequest?
): Boolean {
Timber.tag(TAG).d("OverrideUrlLoading %s", request?.url.toString())
val url = request?.url?.toString()
if (url != null && (url.startsWith("https://") || url.startsWith("http://"))) {
if (time > 0 && System.currentTimeMillis() - time < 200) {
finish()
} else {
view?.loadUrl(url)
}
}
return true
}
}
本文转载自: https://blog.csdn.net/qq_27247815/article/details/140237366
版权归原作者 时空掠影 所有, 如有侵权,请联系我们删除。
版权归原作者 时空掠影 所有, 如有侵权,请联系我们删除。