25

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
  • 703
  • 8
  • 25
Siarhei
  • 2,188
  • 3
  • 22
  • 60

3 Answers3

39

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,188
  • 3
  • 22
  • 60
  • 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
4

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
  • 562
  • 3
  • 18
  • please share the issue you are having – Muhammad Shuja Jun 25 '21 at 02:17
  • If the app has the permissions, the webview won't ask any other permissions? – c-an Jun 25 '21 at 03:31
  • app permissions are not going to be enough, you need to grant permissions for webview as well, check the code above where webchromeclient is granted permissions `webview.setWebChromeClient(..){onPermissionRequest()}` – Muhammad Shuja Jun 25 '21 at 04:57
  • Yes, So, if you implement that part, it passes app's permissions to the webview. And the user doesn't need to be asked twice about the same permissions. Am I right? – c-an Jun 25 '21 at 07:04
  • 1
    yes user should not be asked again if app permissions are granted already – Muhammad Shuja Jun 25 '21 at 14:25
0

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