尝试记录通过加载https://addpipe.com/simple-recorderjs-demo/的android Webview应用程序输入的用户音频时,Javascript引发“权限被拒绝”
试图像这样请求和实现声音/录音权限
清单文件:
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-feature android:name="android.hardware.audio.low_latency" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-feature android:name="android.hardware.audio.pro" />
<uses-feature android:name="android.hardware.microphone"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
MyWebViewClient:
class MyWebViewClient extends WebViewClient {
@SuppressWarnings("deprecation")
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Uri uri = Uri.parse(url);
if (uri.getHost() != null && uri.getHost().contains("addpipe.com")) {
return false;
}
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
主要活动
public class MainActivity extends Activity {
private static final int MY_PERMISSIONS_REQUEST_MODIFY_AUDIO_SETTINGS = 1;
private static final int MY_PERMISSIONS_REQUEST_RECORD_AUDIO = 2;
private static final int MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE;
private WebView mWebView;
@SuppressLint("SetJavaScriptEnabled")
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = findViewById(R.id.activity_main_webview);
// Force links and redirects to open in the WebView instead of in a browser
mWebView.setWebViewClient(new WebViewClient());
// Enable Javascript
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
// REMOTE RESOURCE
mWebView.loadUrl("https://addpipe.com/simple-recorderjs-demo/");
mWebView.setWebChromeClient(new WebChromeClient());
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.MODIFY_AUDIO_SETTINGS)
!= PackageManager.PERMISSION_GRANTED) {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.MODIFY_AUDIO_SETTINGS},
MY_PERMISSIONS_REQUEST_MODIFY_AUDIO_SETTINGS);
}
if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.RECORD_AUDIO)
!= PackageManager.PERMISSION_GRANTED) {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.RECORD_AUDIO},
MY_PERMISSIONS_REQUEST_RECORD_AUDIO);
} if (ContextCompat.checkSelfPermission(MainActivity.this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
// No explanation needed; request the permission
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_MODIFY_AUDIO_SETTINGS: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
System.out.println("FAILED MY_PERMISSIONS_REQUEST_MODIFY_AUDIO_SETTINGS");
}
return;
}case MY_PERMISSIONS_REQUEST_RECORD_AUDIO: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
System.out.println("FAILED MY_PERMISSIONS_REQUEST_RECORD_AUDIO");
}
return;
}case MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
} else {
System.out.println("FAILED MY_PERMISSIONS_REQUEST_EXTERNAL_STORAGE");
}
return;
}
}
}
我遵循了所有我能找到的有关包括并请求必要权限(RECORD_AUDIO,INTERNET,MODIFY_AUDIO_SETTINGS,hardware.microphone等)的建议。
如果我下载了简单的录音机并将其设置为我自己的项目(https://github.com/addpipe/simple-recorderjs-demo),然后尝试将其加载到Webview中,则在单击“记录”按钮时,我会收到Javascript异常“权限被拒绝”。