25

我正在尝试在 中显示WebRTC聊天WebView。根据这个文档WebView v36支持WebRTC。对于我的测试,我使用的是一个设备,Chrome/39.0.0.0并且我已经向AndroidManifest.xml文件添加了权限:

<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" />

但是当我进入聊天室时,我看到了 Chromium 错误日志(设备不显示\翻译任何内容,只有“加载”进度条)

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

在真机 Android 5.1.1 上测试。

4

2 回答 2

39

需要额外的权限请求

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

更新但它不适用于音频捕获

更新在这里找到了工作谷歌示例代码

于 2016-08-12T13:06:16.013 回答
3

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);
于 2021-06-17T17:29:57.890 回答