6

I have a webview that has html video inside it. I want to show this video fullscreen so I override onShowCustomView of my WebChromeClient to use a VideoView. This works great in 2.3, however, in 4.x onShowCustomView is never called. The video will still play, however, it is played from within the webview without any controls besides clicking for play and stop.

Also, I have hardwareAccelerated="true".

Any idea why onShowCustomView is never called?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);

    webView = (WebView) findViewById(R.id.webView);

    webView.getSettings().setSupportZoom(true);
    webView.getSettings().setLoadWithOverviewMode(true);
    webView.getSettings().setUseWideViewPort(true);
    webView.getSettings().setBuiltInZoomControls(true);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.getSettings().setDomStorageEnabled(true);
    webView.getSettings().setPluginState(WebSettings.PluginState.ON);
    webView.setWebViewClient(new WebViewClient());
    webView.setWebChromeClient(new MyChromeClient());

    webView.loadUrl(URL);

}

private class MyChromeClient extends WebChromeClient implements
        OnCompletionListener, OnErrorListener, OnPreparedListener {

    @Override
    public void onShowCustomView(View view, CustomViewCallback callback) {
        Log.d("ma", "onShowCustomView");
    }

...
Nick
  • 6,375
  • 5
  • 36
  • 53

2 Answers2

9

Well after struggling with this for a while, I finally found the cause. In Android 4.x you must show the controls by using the html 'controls' attribute in the 'video' tag. Once you are showing these controls, you can click on the fullscreen button which will then call 'onShowCustomView'. Since embedded video is available in 4.x and you have the option to go to fullscreen with the controls, onShowCustomView will not be called automatically on play. Unfortunately, this is very poorly documented in the Android documentation.

Nick
  • 6,375
  • 5
  • 36
  • 53
  • 4
    Is not any other way to get `onShowCustomView` called? I need the reference to `VideoView` object, but after 4.x I am not able to obtain it – manelizzard Oct 31 '12 at 17:22
  • 2
    Even if you manage to get onShowCustomView called, note that in 4.x what gets passed is not a VideoView anymore but instead a SurfaceView – Bilthon Sep 03 '13 at 14:24
0

This works for me.

I found a 'quirks solution' in making the web app with vimeo videos. I tested two devices. One is Android 4.2.x version, another is 4.4.x version. One is able to play video in fullscreen mode, another is not able to.

After reading official document 'Migrating to WebView in Android 4.4', I found that there are different 'UserAgent' names in two devices.

One has this userAgent.

Mozilla/5.0 (Linux; Android 4.4.2; SHV-E300L Build/KOT49H) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/30.0.0.0 Mobile Safari/537.36

Another has this.

Mozilla/5.0 (Linux; Android 4.4.4; SHV-E370K Build/KTU84P) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/33.0.0.0 Mobile Safari/537.36

I think the chrome version is the solution making fullscreen video. So I changed the chrome version to 30.x.

WebSettings s = mWebView.getSettings();
//Change UserAgent to play fullscreen vimeo's videos.
String agent = s.getUserAgentString();
String p = "(Chrome/[0-9]+\\.[0-9]+\\.[0-9]+\\.[0-9]+)";
Pattern pattern = Pattern.compile(p);
Matcher matcher = pattern.matcher(agent);
if(matcher.find()) {
    agent = matcher.replaceFirst("Chrome/30.0.0.0");
}
s.setUserAgentString(agent);

Oh~~ I can sleep... (sorry with my poor english)

Brownsoo Han
  • 4,549
  • 3
  • 20
  • 20