在我的网络视图中,视频无法全屏显示。当我按下 YouTube 视频上的全屏按钮时,Chrome 控制台显示以下错误。
Uncaught (in promise) TypeError: fullscreen error
系统
Android 10
Chrome version: 83.0.4103.106
应用程序配置
compileSdkVersion 28
defaultConfig {
minSdkVersion 22
targetSdkVersion 28
}
webviewSettings.setJavaScriptEnabled(true);
webviewSettings.setDomStorageEnabled(true);
webviewSettings.setSupportMultipleWindows(false);
webviewSettings.setJavaScriptCanOpenWindowsAutomatically(false);
该错误与此错误错误945287有关,但它应该在我的版本中修复。是否需要任何其他设置才能在网络视图中全屏显示嵌入视频?
我找到了一个效果很好的答案 Playing HTML5 video on fullscreen in android webview ,但对我来说,它在隐藏自定义视图后破坏了键盘。输入文本框不会触发键盘打开。下面的代码是全屏显示并解决我遇到的问题所需的全部内容。
主要活动:
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.FrameLayout;
public class MainActivity extends AppCompatActivity
{
private WebView webView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new MyChrome());
webView.setWebViewClient(new WebViewClient());
webView.loadUrl("http://www.google.com");
}
private class MyChrome extends WebChromeClient
{
View fullscreen = null;
@Override
public void onHideCustomView()
{
fullscreen.setVisibility(View.GONE);
webView.setVisibility(View.VISIBLE);
}
@Override
public void onShowCustomView(View view, CustomViewCallback callback)
{
webView.setVisibility(View.GONE);
if(fullscreen != null)
{
((FrameLayout)getWindow().getDecorView()).removeView(fullscreen);
}
fullscreen = view;
((FrameLayout)getWindow().getDecorView()).addView(fullscreen, new FrameLayout.LayoutParams(-1, -1));
fullscreen.setVisibility(View.VISIBLE);
}
}
AndroidManifest
<activity
android:name=".MainActivity"
android:configChanges="orientation|screenSize" />
设置此处发布的自定义 WebChromeClient 解决了问题。
试试这个
private var customView: View? = null
private var customViewCallback: CustomViewCallback? = null
override fun onHideCustomView() {
(activity.window.decorView as FrameLayout).removeView(customView)
this.customView = null
activity.window.decorView.systemUiVisibility = View.VISIBLE
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_USER
this.customViewCallback?.onCustomViewHidden()
this.customViewCallback = null
}
override fun onShowCustomView(view: View, callback: CustomViewCallback) {
if (this.customView != null) {
onHideCustomView()
return
}
this.customView = view
this.customViewCallback = callback
(activity.window.decorView as FrameLayout).addView(
this.customView,
FrameLayout.LayoutParams(-1, -1)
)
activity.window.decorView.systemUiVisibility = (
View.SYSTEM_UI_FLAG_LOW_PROFILE
or View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
or View.SYSTEM_UI_FLAG_FULLSCREEN
or View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
)
activity.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
}
}