Yuganka Sharan
Yuganka Sharan

Reputation: 203

Webview unable to load video after visibility change

I am facing a strange issue.

Whenever the webview I am using isn't displayed on the screen, it has a problem in loading a new URL the next time it becomes visible on screen.

What I have tested -

  1. If there are no visibility changes, the webview can load many urls one after the other when i call the loadUrl method.

  2. If at any point it is hidden, either because I set its visibility to GONE, or another view comes on top of it, then it doesn't load after it comes back to being VISIBLE.

One interesting thing, the URL is loading (it has an embed code playing a video) as I can hear the sound of the new video, but the visuals are stuck showing the visuals of the last URL that was loaded.

Am I missing something obvious here?

Upvotes: 2

Views: 656

Answers (2)

Razz
Razz

Reputation: 482

I am not sure how you want to play with visibility but I think you will be able to solve your problem after implementing the following code :

    webView = findViewById(R.id.webview);

    webView.setWebViewClient(new WebViewClient());
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
    webView.setWebChromeClient(new WebChromeClient());
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    webView.loadUrl("https://www.w3schools.com/tags/movie.mp4");//video url

    //checking the webview functionality after hiding and showing webview
    webView.setVisibility(View.GONE);
    webView.setVisibility(View.VISIBLE);

Note: Don't forget to add internet permission in your app's manifest.xaml

<uses-permission android:name="android.permission.INTERNET"/>

Upvotes: 0

Yuganka Sharan
Yuganka Sharan

Reputation: 203

Still couldn't figure out why this was happening. Ended up inflating a new instance of Webview each time I needed it (and destroying the older one). This wasn't a very frequent operation (the need would arise in a wide range of frequencies, ranging from once in 5 seconds, to once in 2 hours) so performance issues could be neglected.

Not a solution, but it worked for me.

Upvotes: 1

Related Questions