Android: WebView - not getting Camera and Mic enabled for conference
Asked Answered
A

2

5

We connect to https://meet.jit.si for video conference. If we use a chrome browser on Android device (tested on both Android 7 and 10), we are able to join with both Camera and Mic enabled. Able to turn them on and off as well. But if we try the same using a webview, we are not even getting a prompt from the website to allow access and get a "Failed to access camera/mic" error when trying to turn them on.

This is our code.

Manifest

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

Getting runtime permission on load

    String[] permissions =
            {Manifest.permission.READ_EXTERNAL_STORAGE,
             Manifest.permission.WRITE_EXTERNAL_STORAGE,
             Manifest.permission.INTERNET,
             Manifest.permission.RECORD_AUDIO,
             Manifest.permission.CAMERA};

    ActivityCompat.requestPermissions(
            this,
            permissions,
            1010);

Loading the webpage into WebView

String url = "https://meet.jit.si/testingconf49854";
WebView webView = findViewById(R.id.webView);
WebSettings mWebSettings = webView.getSettings();
mWebSettings.setLoadsImagesAutomatically(true);
mWebSettings.setBlockNetworkLoads(false);
mWebSettings.setJavaScriptEnabled(true);
mWebSettings.setLoadWithOverviewMode(true);
mWebSettings.setUseWideViewPort(false);
mWebSettings.setTextZoom(70);
mWebSettings.setCacheMode( WebSettings.LOAD_NO_CACHE );
mWebSettings.setUserAgentString("Mozilla/5.0 (Linux; Android 5.1.1; Nexus 5 Build/LMY48B; wv) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/43.0.2357.65 Mobile Safari/537.36");
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(true);

Map<String, String> noCacheHeaders = new HashMap<String, String>(2);
noCacheHeaders.put("Pragma", "no-cache");
noCacheHeaders.put("Cache-Control", "no-cache");
webView.loadUrl(url, noCacheHeaders);

Please let us know what we are missing here. Appreciate your help.

Attraction answered 27/5, 2020 at 7:40 Comment(0)
M
8

After getting those permissions, you need to override onPermissionRequest in WebChromeClient like this:

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

Myrtamyrtaceous answered 27/5, 2020 at 7:43 Comment(4)
Awesome, worked perfectly on both Android 7 and 10. Thank you very much for the precise and prompt help.Attraction
works really well, almost spend many times searching for the answer, thank you very muchBullnose
This is a good answer, but Android disagrees. See the documentation: developer.android.com/reference/android/webkit/…Tse
You need to grant access for each permission requested in accordance with the permissions of your application. You will have ANR if it happens that a certain WEB page of the server through HTML code requests access to resources that are not set in the Manifest file. You also know that the permissions from your Manifesto file must be approved in advance.Tse
H
0

You can try it that way, work perfectly for me. This is my code:

   @Override
           public void onPermissionRequest(PermissionRequest request){
                String[] resources = request.getResources();
                
                switch (resources[0]){
                    case PermissionRequest.RESOURCE_AUDIO_CAPTURE:
                        Toast.makeText(getBaseContext(), "Audio Permission", Toast.LENGTH_SHORT).show();
                        request.grant(new String[]{PermissionRequest.RESOURCE_AUDIO_CAPTURE});
                    break;
                    case PermissionRequest.RESOURCE_MIDI_SYSEX:
                        Toast.makeText(getBaseContext(), "MIDI Permission", Toast.LENGTH_SHORT).show();
                        request.grant(new String[]{PermissionRequest.RESOURCE_MIDI_SYSEX});
                    break;
                    case PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID:
                        Toast.makeText(getBaseContext(), "Encrypted media permission", Toast.LENGTH_SHORT).show();
                        request.grant(new String[]{PermissionRequest.RESOURCE_PROTECTED_MEDIA_ID});
                    break;
                    case PermissionRequest.RESOURCE_VIDEO_CAPTURE:
                        Toast.makeText(getBaseContext(), "Video Permission", Toast.LENGTH_SHORT).show();
                        request.grant(new String[]{PermissionRequest.RESOURCE_VIDEO_CAPTURE});
                    break;
                }
           } 
Housewarming answered 19/11, 2021 at 20:57 Comment(2)
As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.Plumbiferous
This is a great answer, and it agrees with the recommendations of the people from Android. It is only necessary to execute the code in the UI. See their documentation: developer.android.com/reference/android/webkit/…Tse

© 2022 - 2024 — McMap. All rights reserved.