gpt4 book ai didi

java - 在 Android 中使用 WebView 访问麦克风

转载 作者:太空宇宙 更新时间:2023-11-03 10:40:11 33 4
gpt4 key购买 nike

我正在尝试编写一个程序,其中包含一个 WebView,该程序显示一个网页,该网页使用麦克风录制音频(录制是使用 javascript getUserMedia 完成的)。我已经实现了以下代码,我得到了一个弹出窗口,询问用户是否允许,在我允许之后,调用了授权函数(我想我可以访问麦克风),但录音只是空的。如果我在浏览器上尝试同一个网站,那么它就可以工作。

我正在测试 this website .任何帮助,将不胜感激。

@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());
}
}

最佳答案

原来是权限问题。以下两项都需要包含在 list 文件中。

关于java - 在 Android 中使用 WebView 访问麦克风,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35389368/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com