Fullscreen Video button inside WebView not working

I am trying to implement a subclass of WebChromeClient in order to have a working full-screen button on youtube embedded videos (for example: https://www.youtube.com/embed/dQw4w9WgXcQ ) inside a WebView.

I basically simplified this repo , here are some snippets:

VideoWebChromeClient:

public class VideoWebChromeClient extends WebChromeClient { private boolean isVideoFullscreen = false; private View activityNonVideoView; private ViewGroup activityVideoView; private View videoViewContainer; private CustomViewCallback videoCallback; private Window videoWindow; public VideoWebChromeClient(View activityNonVideoView, ViewGroup activityVideoView, Window window) { this.activityNonVideoView = activityNonVideoView; this.activityVideoView = activityVideoView; this.videoWindow = window; } @Override public void onShowCustomView(View view, CustomViewCallback callback) { Log.w("ENTER FULLSCREEN"); videoCallback = callback; videoViewContainer = view; videoWindow.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); activityNonVideoView.setVisibility(View.GONE); activityVideoView.addView(videoViewContainer, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); activityVideoView.setVisibility(View.VISIBLE); isVideoFullscreen = true; } @Override public void onHideCustomView() { if (!isVideoFullscreen) { return; } Log.w("EXIT FULLSCREEN"); activityVideoView.setVisibility(View.GONE); activityVideoView.removeView(videoViewContainer); activityNonVideoView.setVisibility(View.VISIBLE); videoWindow.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON); videoViewContainer = null; videoCallback.onCustomViewHidden(); isVideoFullscreen = false; } public boolean onBackPressed() { onHideCustomView(); return isVideoFullscreen; } } 

WebViewActivity

 public class WebViewActivity extends BaseActivity { public static final String WEB_VIEW_URL_EXTRA = "URL"; private boolean loadedFirstURL = true; private VideoWebChromeClient mWebChromeClient; @Bind(webview) WebView mWebView; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(provideLayoutId()); String url = (String) getIntent().getSerializableExtra(WEB_VIEW_URL_EXTRA); // Check if URL is YouTube / Vimeo if (RegexHelper.isVideoURL(url)){ // Allow Fullscreen final View defaultLayout = findViewById(R.id.defaultLayout); final View fullscreenLayout = findViewById(R.id.fullscreenLayout); mWebChromeClient = new VideoWebChromeClient(defaultLayout, (ViewGroup) fullscreenLayout, getWindow()); mWebView.setWebChromeClient(mWebChromeClient); // Block external links mWebView.setWebViewClient(new WebViewClient() { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { if (loadedFirstURL) { loadedFirstURL = false; return false; } return true; } }); } mWebView.getSettings().setJavaScriptEnabled(true); mWebView.loadUrl(url); } @Override public void onBackPressed() { if (!mWebChromeClient.onBackPressed()) { if (mWebView.canGoBack()) { mWebView.goBack(); } else { super.onBackPressed(); } } } } 

It works correctly on KitKat , but not on Nougat (I only have two devices that need to be checked): when I click the full-screen view button, it sometimes enters full-screen mode, but more often it β€œblinks” and remains not in full-screen mode.

And then, when it really is in full screen mode, the exit button from full screen mode does not work ( onHideCustomView is not called at all)

You can see that I placed the logs and noticed that when a "flash" occurs, onHideCustomView actually called right after onShowCustomView ( "ENTER FULLSCREEN" and then "EXIT FULLSCREEN" ), I don’t understand why.

Thanks so much if anyone can point me to a solution

+5
source share
1 answer

I also ran into the same problem, but I fixed it by spending hours on it.

All you have to do is install the KitKat device UserAgent string. And it's all!

 private String userAgent = "Mozilla/5.0 (Linux; Android 4.4; Nexus 5 Build/_BuildID_) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36"; mWebView.getSettings().setUserAgentString(userAgent); 

This will eliminate all the strange functioning of onShowCustomView () and onHideCustomView () on new devices. Hope this helps.

0
source

Source: https://habr.com/ru/post/1268266/


All Articles