Access to microphone with WebView in Android
Asked Answered
H

1

6

I am trying to write a program that has a WebView in it that shows a webpage which records audio using the microphone (recording is done using javascript getUserMedia). I have implemented the following code, and I get the pop up that asks the user for permission and after I allow, the grant function is called (and I think I get access to the mic), but the recording is just empty. If I try the same website on the browser, then it works.

I am testing with this website. Any help would be appreciated.

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
            // If request is cancelled, the result arrays are empty.
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {


                // permission was granted, yay! Do the
                // contacts-related task you need to do.
                myCallback.invoke(myOrigin, true, false);

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
                Log.d("WebView", "PERMISSION NOT GRANTED");
            }
            return;
        } case MY_PERMISSIONS_REQUEST_RECORD_AUDIO: {
            Log.d("WebView", "PERMISSION FOR AUDIO");
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {


                // permission was granted, yay! Do the
                // contacts-related task you need to do.
                myRequest.grant(myRequest.getResources());

            } else {

                // permission denied, boo! Disable the
                // functionality that depends on this permission.
            }
        }

        // other 'case' lines to check for other
        // permissions this app might request
    }
}

and in my ChromeClient I have :

@Override
    public void onPermissionRequest(final PermissionRequest request) {
        myRequest = request;

        for(String permission : request.getResources()) {
            switch(permission) {
                case "android.webkit.resource.AUDIO_CAPTURE": {
                    askForPermission(request.getOrigin().toString(), Manifest.permission.RECORD_AUDIO, MY_PERMISSIONS_REQUEST_RECORD_AUDIO);
                    break;
                }
            }
        }
    }

public void askForPermission(String origin, String permission, int requestCode) {
        Log.d("WebView", "inside askForPermission for" + origin + "with" + permission);

        if (ContextCompat.checkSelfPermission(getApplicationContext(),
                permission)
                != PackageManager.PERMISSION_GRANTED) {

            // Should we show an explanation?
            if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
                   permission)) {

                // Show an expanation to the user *asynchronously* -- don't block
                // this thread waiting for the user's response! After the user
                // sees the explanation, try again to request the permission.

            } else {

                // No explanation needed, we can request the permission.

                ActivityCompat.requestPermissions(thisActivity,
                        new String[]{permission},
                        requestCode);
            }
        } else {
            myRequest.grant(myRequest.getResources());
        }
    }
Heuristic answered 14/2, 2016 at 7:22 Comment(2)
in your manifest add these two permissions <uses-permission android:name="android.permission.RECORD_AUDIO" /> <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />Distortion
not working i used the sameStamper
H
1

This turned out to be an issue with the permissions. Both of the following needs to be included in the manifest file.

Heuristic answered 17/2, 2016 at 17:22 Comment(1)
Which permissions are required to add in manifest to capture audio from WebVIew, Video is working fine but i am not able to capture audio.Crandell

© 2022 - 2024 — McMap. All rights reserved.