Nick
Nick

Reputation: 6385

Force WebView Video Controls

I have an Android app that plays embedded html5 videos. The html code that I receive does not include the "controls" attribute in the video tag. I'm wondering if there is a way to force the controls to be shown while the video is in the WebView. In 2.3 you can use WebChromeClient to display the full video. However in 4.x it appears you need to show the full controls first in order to take advantage of this class.

Upvotes: 0

Views: 1467

Answers (1)

dokkaebi
dokkaebi

Reputation: 9190

You could add the controls attribute:

webView.getSettings().setJavascriptEnabled(true);
webView.setWebViewClient(new WebViewClient() {
    @Override
    public void onPageFinished(WebView webView, String url) {
        super.onPageFinished(webView, url);
        webView.loadUrl(
            "javascript:(function() {" +
                 "var video = document.getElementsByTagName('video')[0];" +
                 "video.controls = 'controls';" +
            "})()"
        );
    }
});

Upvotes: 1

Related Questions