Android:WebView - 没有为会议启用摄像头和麦克风
Android: WebView - not getting Camera and Mic enabled for conference
我们连接到 https://meet.jit.si 进行视频会议。如果我们在 Android 设备上使用 chrome 浏览器(在 Android 7 和 10 上测试),我们可以在启用摄像头和麦克风的情况下加入。也可以打开和关闭它们。但是,如果我们使用 webview 尝试相同的操作,我们甚至不会从网站获得允许访问的提示,并且在尝试打开它们时会出现 "Failed to access camera/mic" 错误。
这是我们的代码。
清单
<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" />
正在获取加载运行时权限
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);
正在将网页加载到 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);
请告诉我们这里缺少什么。感谢您的帮助。
获得这些权限后,您需要像这样覆盖 WebChromeClient
中的 onPermissionRequest
:
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onPermissionRequest(final PermissionRequest request) {
request.grant(request.getResources());
}
});
你可以这样试试,对我来说效果很好。这是我的代码:
@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;
}
}
我们连接到 https://meet.jit.si 进行视频会议。如果我们在 Android 设备上使用 chrome 浏览器(在 Android 7 和 10 上测试),我们可以在启用摄像头和麦克风的情况下加入。也可以打开和关闭它们。但是,如果我们使用 webview 尝试相同的操作,我们甚至不会从网站获得允许访问的提示,并且在尝试打开它们时会出现 "Failed to access camera/mic" 错误。
这是我们的代码。
清单
<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" />
正在获取加载运行时权限
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);
正在将网页加载到 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);
请告诉我们这里缺少什么。感谢您的帮助。
获得这些权限后,您需要像这样覆盖 WebChromeClient
中的 onPermissionRequest
:
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onPermissionRequest(final PermissionRequest request) {
request.grant(request.getResources());
}
});
你可以这样试试,对我来说效果很好。这是我的代码:
@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;
}
}