How to Enable protected content in a webview?

Viewed 4217

In chrome browser, there is an option to play protected content. How do I enable the same in webview in android?

I have tried a method called allowContentAccess() but that does not work.

Please help

2 Answers

To allow a webview to play a DRM content you have to grant RESOURCE_PROTECTED_MEDIA_ID permission to the webview.

You can do it by override of WebChromeClient#onPermissionRequest

As a sample:

    @Override
    public void onPermissionRequest(PermissionRequest request) {
        String[] resources = request.getResources();
        for (int i = 0; i < resources.length; i++) {
            if (PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID.equals(resources[i])) {
                request.grant(resources);
                return;
            }
        }

        super.onPermissionRequest(request);
    }

setAllowContentAccess() (I couldn't find the allowContentAccess() you mentioned) is used for accessing local content files on the device, not for protected content.

Protected content viewing enabling is usually controlled by the user, not the developer unfortunately. For example, for the default browser Chrome according to this help article:

Chrome will play protected content by default.

If you don't want Chrome to play protected content by default, you can change your settings:

  1. On your Android phone or tablet, open the Chrome app Chrome.
  2. To the right of the address bar, tap More More and then Settings.
  3. Tap Site settings and then Media and then Protected Content.
  4. Select Ask first.

Since accessing protected content requires the site viewing information about your device, I expect this isn't available through a WebView.

There is a full built-in framework available (since API level 11) for managing DRM content that should be used instead.

Related