简体   繁体   English

Android webview 录音不起作用

[英]Android webview audio recording does not work

Voice recorder does not work in my webview application, which I write with Java.录音机在我用 Java 编写的 webview 应用程序中不起作用。 Please help.请帮忙。 I don't know how to get a voice recording permit.我不知道如何获得录音许可证。 here is my code:这是我的代码:

her is the complete code https://drive.google.com/file/d/1Ov5BesMJN7aXF9IGA-mJO0FObIqH4pWr/view?usp=sharing她是完整的代码https://drive.google.com/file/d/1Ov5BesMJN7aXF9IGA-mJO0FObIqH4pWr/view?usp=sharing

    webView.setWebViewClient(new WebViewClient() {



        public void onReceivedError(WebView webView, int errorCode, String description, String failingUrl) {
            try {
                webView.stopLoading();
            } catch (Exception e) {
            }

            if (webView.canGoBack()) {
                webView.goBack();
            }

            webView.loadUrl("about:blank");
            Intent intent2 = new Intent(Main2Activity.this,Connection.class);
            startActivity(intent2);
            finish();
            super.onReceivedError(webView, errorCode, description, failingUrl);
        }


        public boolean shouldOverrideUrlLoading(WebView view, String url) {

            if (url.contains(".pdf")) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.parse(url), "application/pdf");
                try {
                    view.getContext().startActivity(intent);
                } catch (ActivityNotFoundException e) {

                }
            }

            else {
                webView.loadUrl(url);
            }
            return false;

you need runtime permission to allow voice recording here is the code您需要运行时权限才能允许录音,这里是代码

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
    if (ContextCompat.checkSelfPermission(this, Manifest.permission.RECORD_AUDIO) ==
            PackageManager.PERMISSION_GRANTED) {
        // put your code for Version>=Marshmallow
    } else {
        if (shouldShowRequestPermissionRationale(Manifest.permission.RECORD_AUDIO)) {
            Toast.makeText(this,
                    "App required access to audio", Toast.LENGTH_SHORT).show();
        }
        requestPermissions(new String[]{Manifest.permission.RECORD_AUDIO
        }, REQUEST_CAMERA_PERMISSION_RESULT);
    }

} else {
    // put your code for Version < Marshmallow
}

and override onRequestPermissionsResult并覆盖 onRequestPermissionsResult

 @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); if (requestCode == REQUEST_AUDIO_PERMISSION_RESULT) { if (grantResults[0] != PackageManager.PERMISSION_GRANTED) { Toast.makeText(getApplicationContext(), "Application will not have audio on record", Toast.LENGTH_SHORT).show(); } } }

I have run your code.the permission for audio record is working fine.you have to also take external storage permission, to do this add this line to menifest file <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> .then change permission method to this我已经运行你的代码。音频记录的权限工作正常。你还必须获得外部存储权限,要做到这一点,将此行添加到清单文件<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> .then 将权限方法更改为此

private void requestAudioPermissions() {
        if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.RECORD_AUDIO )
                != PackageManager.PERMISSION_GRANTED) {

            //When permission is not granted by user, show them message why this permission is needed.
            if (ActivityCompat.shouldShowRequestPermissionRationale(this,
                    Manifest.permission.RECORD_AUDIO)) {
                Toast.makeText(this, "Please grant permissions to record audio", Toast.LENGTH_LONG).show();

                //Give user option to still opt-in the permissions
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.RECORD_AUDIO,Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_RECORD_AUDIO);

            } else {
                // Show user dialog to grant permission to record audio
                ActivityCompat.requestPermissions(this,
                        new String[]{Manifest.permission.RECORD_AUDIO,Manifest.permission.WRITE_EXTERNAL_STORAGE},
                        MY_PERMISSIONS_RECORD_AUDIO);
            }
        }
        //If permission is granted, then go ahead recording audio
        else if (ContextCompat.checkSelfPermission(this,
                Manifest.permission.RECORD_AUDIO)
                == PackageManager.PERMISSION_GRANTED) {

            //Go ahead with recording audio now

        }
    } 

and make another the warning when click on microphone is from your website.check again in your site what is the problem there.并在单击麦克风来自您的网站时发出另一个警告。再次检查您的网站有什么问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM