31

I'm trying to show WebRTC chat in WebView. According to this documentation, WebView v36 supports WebRTC. For my test I'm using a device with Chrome/39.0.0.0 and I have added permissions to the AndroidManifest.xml file:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<user-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

but when I enter into the chat, I see a Chromium error log (device doesn't show \ translate anything, only 'loading' progress bar):

W/AudioManagerAndroid: Requires MODIFY_AUDIO_SETTINGS and RECORD_AUDIO
W/AudioManagerAndroid: No audio device will be available for recording
E/chromium: [ERROR:web_contents_delegate.cc(178)] WebContentsDelegate::CheckMediaAccessPermission: Not supported.
E/chromium: [ERROR:web_contents_delegate.cc(178)] WebContentsDelegate::CheckMediaAccessPermission: Not supported.
W/AudioManagerAndroid: Requires MODIFY_AUDIO_SETTINGS and RECORD_AUDIO
W/AudioManagerAndroid: No audio device will be available for recording
D/ChromiumCameraInfo: Camera enumerated: front

Tested on a real device, Android 5.1.1.

Harry Theo
  • 784
  • 1
  • 8
  • 28
Siarhei
  • 2,358
  • 3
  • 27
  • 63

4 Answers4

46

additional request for permissions is needed

webView.setWebChromeClient(new WebChromeClient(){
        @TargetApi(Build.VERSION_CODES.LOLLIPOP)
        @Override
        public void onPermissionRequest(final PermissionRequest request) {
                request.grant(request.getResources());
        }
    });

update but it not working for audio capture

UPDATE found working google-sample code here

Siarhei
  • 2,358
  • 3
  • 27
  • 63
  • Any suggestion for me : https://stackoverflow.com/questions/44180093/cant-access-camera-from-android-webview-chrome-frame-in-context-of-webrtc – Nikola Lukic May 29 '17 at 11:45
  • @NikolaLukic hi, have you tried https://github.com/GoogleChrome/chromium-webview-samples/blob/master/webrtc-example/app/src/main/java/com/google/chrome/android/webrtcsample/MainActivity.java this sample? – Siarhei May 29 '17 at 14:59
  • Yes this project works but i still wanna know what is the problem with my project . Diff is only that i not use drawer layout and fragment layout ?!? thanks – Nikola Lukic May 30 '17 at 08:42
  • Hello @JonathanDunlap audio capture works in google-sample that i've shared in the answer – Siarhei Jul 25 '18 at 08:28
  • So where exactly do you add that code above, as in what file? – Johnny Gasyna Aug 01 '19 at 04:55
  • Hello @JohnnyGasyna, as i remember i've just replace my code with provided in sample. I'm not working on this project now and not able to check :( – Siarhei Aug 01 '19 at 13:29
  • 3
    @NikolaLukic: can you please tell me how did you solve ? I tried same in my project, but it's not working. – Sagar Panwala Apr 05 '20 at 08:24
  • I have these permissions, but still getting error ```WebContentsDelegate::CheckMediaAccessPermission: Not supported.``` – Sujith S Manjavana Jul 02 '22 at 16:57
8

You need these permissions to access Camera and Microphone

<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="true"/>
<uses-feature android:name="android.hardware.camera.autofocus"/>
<uses-permission android:name="android.permission.RECORD_AUDIO" />

// don't miss this one
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" /> 

Next you need to grant permissions to your webview, check this link for more details:

webView.setWebChromeClient(new WebChromeClient(){
        @Override
        public void onPermissionRequest(PermissionRequest request) {
            runOnUiThread(() -> {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    String[] PERMISSIONS = {
                            PermissionRequest.RESOURCE_AUDIO_CAPTURE,
                            PermissionRequest.RESOURCE_VIDEO_CAPTURE
                    };
                    request.grant(PERMISSIONS);
                }
            });
        }
    });

If audio playback is not working, use this:

webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
Muhammad Shuja
  • 642
  • 5
  • 18
2

My experience with this in 2022:

  1. CAMERA and RECORD_AUDIO permissions need to be declared in Manifest
  2. setWebChromeClient.onPermissionRequest should check if those permissions have already been granted. If not, use registerForActivityResult(new RequestMultiplePermissions()) to ask the user to grant them.
Sarsaparilla
  • 6,300
  • 1
  • 32
  • 21
1

its mostly error in webview reload becuase when we will request for audio , camera permission on webview , after accept permission , we need to refresh the webpage.

   if (permission.equals("android.webkit.resource.AUDIO_CAPTURE")) {
                        demandForPermission(request.getOrigin().toString(), Manifest.permission.RECORD_AUDIO, MY_PERMISSIONS_REQUEST_RECORD_AUDIO);
                    } else {
                        myRequest.grant(request.getResources());
                    }

I also stuck this problem for many days but after in below link code , 100% working code Android Webview