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.
  1. 打开硬件加速(3.0以上版本支持)
  2. set一个WebChromClient,实现onShowCustomView() 方法和onHideCustomView()方法
  3. 全屏支持

二、实现解决

  1. 打开硬件加速
  • 在Manifest中,对应的Activity添加: android:hardwareAccelerated = “true”。
  • 防止h5重新加载:Manifest中,对应的Activity添加: android:configChanges=“keyboardHidden|orientation|screenSize”
  • 切换横屏时,屏幕的H5内容始终以竖屏显示:Manifest中,对应的Activity添加: android:screenOrientation=“portrait”
<activity
    android:name=".uicomponent.WebViewActivity"
    android:configChanges="orientation|screenSize|keyboardHidden" //防止h5重新加载
    android:hardwareAccelerated="true" //硬件加速
    android:screenOrientation="portrait" //当我们切换横竖屏的时候,屏幕的内容始终以竖屏显示,而不会根据屏幕的方向来显示内容
    android:theme="@style/AppTheme.NoActionBar" />
  1. WebView中设置WebChromClient实现接口onShowCustomView() 方法和onHideCustomView()方法, 实现后即显示全屏播放按钮,但是点击无反应,需要实现全屏支持。

  2. 全屏支持实现: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

<FrameLayout
    android:id="@+id/web_root"
    app:layout_constraintBottom_toTopOf="@id/v_bottom"
    app:layout_constraintTop_toBottomOf="@id/v_top"
    android:layout_width="match_parent"
    android:layout_height="0dp">

   <WebView
         android:id="@+id/webview"
         android:layout_width="match_parent"
         android:layout_height="match_parent" />
   
    <ProgressBar
        android:id="@+id/progress_bar"
        style="?android:progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="1.5dp"
        android:max="100"
        android:progressDrawable="@drawable/webview_progress_bar_style" />
</FrameLayout>

三、写在最后

此文章为个人开发时记录,有时时间有限,无法深入研究,若看到此文章后有其他见解或解决方式,欢迎留言交流👇👇👇

————————————————
版权声明:转载请附上原文出处链接及本声明。
原文链接:
https://blog.csdn.net/weixin_44158429/article/details/130217214

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值