Android WebView 加载H5网页 全屏幕播放视频
程序员文章站
2022-05-23 14:16:18
...
#Android WebView 加载H5网页 全屏幕播放视频
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:visibility="gone"
android:id="@+id/fl_video"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<WebView
android:id="@+id/web_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
# 设置WebChromeClient
web_view.webChromeClient = object : WebChromeClient() {
override fun onShowCustomView(view: View?, callback: CustomViewCallback?) {
super.onShowCustomView(view, callback)
if (mCustomView != null) {
callback?.onCustomViewHidden()
return
}
mCustomView = view
mCustomView?.visibility = View.VISIBLE
mCustomViewCallBack = callback
fl_video.addView(mCustomView)
fl_video.visibility = View.VISIBLE
fl_video.bringToFront()
//设置横屏
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
}
override fun onHideCustomView() {
super.onHideCustomView()
if (mCustomView == null) {
return
}
mCustomView?.visibility = View.GONE
fl_video.removeView(mCustomView)
mCustomView = null
fl_video.visibility = View.GONE
mCustomViewCallBack?.onCustomViewHidden()
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
}
// 检测 屏幕旋转
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
window.clearFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN)
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
window.addFlags(WindowManager.LayoutParams.FLAG_FORCE_NOT_FULLSCREEN);
}
}
上一篇: Collection
推荐阅读
-
解决Android WebView拦截url,视频播放加载失败的问题
-
Android使用WebView实现全屏切换播放网页视频功能
-
Android开发如何加载WebView中的H5页面并全屏视频播放
-
Android中使用WebView实现全屏切换播放网页视频
-
Android Webview加载各种网络视频 播放暂停播放完成监听事件
-
解决Android WebView拦截url,视频播放加载失败的问题
-
Android Webview加载网页视频播放遇到的坑
-
Android WebView 加载H5网页 全屏幕播放视频
-
Android使用WebView实现全屏切换播放网页视频功能
-
Android中使用WebView实现全屏切换播放网页视频