Parth Maitra
Parth Maitra

Reputation: 61

How to Enable protected content in a webview?

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

Upvotes: 5

Views: 11329

Answers (3)

Lucas P.
Lucas P.

Reputation: 4532

Following Viacheslav's answer which works perfectly, here's the same snippet in Kotlin, to allow the WebView to play DRM content:

        override fun onPermissionRequest(request: PermissionRequest?) {
            request?.let { request ->
                if (request.resources.contains(PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID)) {
                    request.grant(request.resources)
                    return
                }
            }

            super.onPermissionRequest(request)
        }

Upvotes: 0

Viacheslav Smityukh
Viacheslav Smityukh

Reputation: 5843

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);
    }

Upvotes: 8

Jake Lee
Jake Lee

Reputation: 7989

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.

Upvotes: 0

Related Questions