🔍
📢

Android WebView自动允许WebRTC摄像头权限和解决http问题

需要Android 5.0+

 

向AndroidManifest.xml中加入权限:

<uses-permission android:name="android.permission.INTERNET"/>
 
<uses-permission android:name="android.permission.CAMERA" />
 
<uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT" />

 

自动允许权限:

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

    MainActivity.this.runOnUiThread(new Runnable(){


      @TargetApi(Build.VERSION_CODES.LOLLIPOP)
      @Override
      public void run() {
        request.grant(request.getResources());
      }


    });
  }
});

 

webrtc音视频聊天直接跳过https协议报错:

public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
    handler.proceed();
}