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”

android:name=".uicomponent.WebViewActivity"

android:configChanges="orientation|screenSize|keyboardHidden" //防止h5重新加载

android:hardwareAccelerated="true" //硬件加速

android:screenOrientation="portrait" //当我们切换横竖屏的时候,屏幕的内容始终以竖屏显示,而不会根据屏幕的方向来显示内容

android:theme="@style/AppTheme.NoActionBar" />

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

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">

android:id="@+id/webview"

android:layout_width="match_parent"

android:layout_height="match_parent" />

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" />

三、写在最后

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

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

相关文章

评论可见,请评论后查看内容,谢谢!!!
 您阅读本篇文章共花了: