cprcrack / VideoEnabledWebView

Android's WebView and WebChromeClient class extensions that enable fully working HTML5 video support
MIT License
1.05k stars 289 forks source link

Back button is not working properly #26

Open brinder opened 7 years ago

brinder commented 7 years ago

When the back is pressed from fullscreen while a video is playing, video gets struck.

Myerden commented 7 years ago

To accomplish this feature, we should simulate the 'exit full screen' click from webView. What I mean, when user press back button , it should exit the fullscreen like clicked the video's fullscreen button. to do this follow these steps;

First Step

Embed this javascript code inside your webView element.

<script>
function exitFullScreen() {
    if (document.exitFullscreen) {
      document.exitFullscreen();
    } else if (document.msExitFullscreen) {
      document.msExitFullscreen();
    } else if (document.mozCancelFullScreen) {
      document.mozCancelFullScreen();
    } else if (document.webkitExitFullscreen) {
      document.webkitExitFullscreen();
    }
}
</script>

** this code should be inside the html body tag.

Second Step

Use boolean value to know if the video is in fullscreen mode.

My activity like this;

public class PostDetailActivity extends AppCompatActivity 
    implements VideoEnabledWebChromeClient.ToggledFullscreenCallback{

private boolean fullscreen = false;
// some code

in here we save the fullscreen situation

@Override
public void toggledFullscreen(boolean fullscreen) {
    this.fullscreen = fullscreen;
    // Your code to handle the full-screen change, for example showing and hiding the title bar. Example:
    if (fullscreen)
    {

Third Step

Call this javascript exit function from activity when back button is pressed;

@Override
public void onBackPressed() {
    if (fullscreen){
        postWebView.loadUrl("javascript:toggleFullScreen()");
        fullscreen=false;
        //webChromeClient.onHideCustomView();
    }
    else
    {
        postWebView.onPause();
        super.onBackPressed();
    }
}

https://stackoverflow.com/a/21281249/4133449