我正在尝试编写一个程序,其中包含一个WebView,它显示使用麦克风录制音频的网页(使用javascript中的
我正在使用这个网站进行测试。任何帮助都将不胜感激。
在我的ChromeClient中,我有以下内容:
getUserMedia
进行录制)。我已经实现了以下代码,并获得了请求用户权限的弹出窗口,在我允许后,授权函数被调用(我认为我已经获得了麦克风的访问权限),但是录音为空。如果我在浏览器上尝试同样的网站,则可以正常录音。我正在使用这个网站进行测试。任何帮助都将不胜感激。
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
myCallback.invoke(myOrigin, true, false);
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
Log.d("WebView", "PERMISSION NOT GRANTED");
}
return;
} case MY_PERMISSIONS_REQUEST_RECORD_AUDIO: {
Log.d("WebView", "PERMISSION FOR AUDIO");
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
myRequest.grant(myRequest.getResources());
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
}
// other 'case' lines to check for other
// permissions this app might request
}
}
在我的ChromeClient中,我有以下内容:
@Override
public void onPermissionRequest(final PermissionRequest request) {
myRequest = request;
for(String permission : request.getResources()) {
switch(permission) {
case "android.webkit.resource.AUDIO_CAPTURE": {
askForPermission(request.getOrigin().toString(), Manifest.permission.RECORD_AUDIO, MY_PERMISSIONS_REQUEST_RECORD_AUDIO);
break;
}
}
}
}
public void askForPermission(String origin, String permission, int requestCode) {
Log.d("WebView", "inside askForPermission for" + origin + "with" + permission);
if (ContextCompat.checkSelfPermission(getApplicationContext(),
permission)
!= PackageManager.PERMISSION_GRANTED) {
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
permission)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(thisActivity,
new String[]{permission},
requestCode);
}
} else {
myRequest.grant(myRequest.getResources());
}
}