一、官方介紹
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.
- 打開(kāi)硬件加速(3.0以上版本支持)
- set一個(gè)WebChromClient,實(shí)現(xiàn)onShowCustomView() 方法和onHideCustomView()方法
- 全屏支持
二、實(shí)現(xiàn)解決
- 打開(kāi)硬件加速
- 在Manifest中,對(duì)應(yīng)的Activity添加: android:hardwareAccelerated = “true”。
- 防止h5重新加載:Manifest中,對(duì)應(yīng)的Activity添加: android:configChanges=“keyboardHidden|orientation|screenSize”
- 切換橫屏?xí)r,屏幕的H5內(nèi)容始終以豎屏顯示:Manifest中,對(duì)應(yīng)的Activity添加: android:screenOrientation=“portrait”
<activity
android:name=".uicomponent.WebViewActivity"
android:configChanges="orientation|screenSize|keyboardHidden" //防止h5重新加載
android:hardwareAccelerated="true" //硬件加速
android:screenOrientation="portrait" //當(dāng)我們切換橫豎屏的時(shí)候,屏幕的內(nèi)容始終以豎屏顯示,而不會(huì)根據(jù)屏幕的方向來(lái)顯示內(nèi)容
android:theme="@style/AppTheme.NoActionBar" />
-
WebView中設(shè)置WebChromClient實(shí)現(xiàn)接口onShowCustomView() 方法和onHideCustomView()方法, 實(shí)現(xiàn)后即顯示全屏播放按鈕,但是點(diǎn)擊無(wú)反應(yīng),需要實(shí)現(xiàn)全屏支持。
-
全屏支持實(shí)現(xiàn):WebView在點(diǎn)擊全屏按鈕后調(diào)用onShowCustomView方法,而全屏的視頻會(huì)在其參數(shù)view中進(jìn)行渲染。我們需要在Activity中寫(xiě)一個(gè)viewRoot,在onShowCustomView觸發(fā)后,將其view傳入viewRoot,且使APP橫屏,達(dá)到全屏顯示。
@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) { //當(dāng)上一個(gè)view存在時(shí),隱藏全屏
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) { //當(dāng)上一個(gè)view不存在時(shí),不處理
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>
三、寫(xiě)在最后
此文章為個(gè)人開(kāi)發(fā)時(shí)記錄,有時(shí)時(shí)間有限,無(wú)法深入研究,若看到此文章后有其他見(jiàn)解或解決方式,歡迎留言交流??????文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-493111.html
————————————————
版權(quán)聲明:轉(zhuǎn)載請(qǐng)附上原文出處鏈接及本聲明。
原文鏈接:
https://blog.csdn.net/weixin_44158429/article/details/130217214文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-493111.html
到了這里,關(guān)于Android WebView H5視頻播放實(shí)現(xiàn)全屏播放功能、全屏按鈕不顯示、灰顯、點(diǎn)擊無(wú)效問(wèn)題解決方案的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!