简体   繁体   中英

Android webview audio recording does not work

Voice recorder does not work in my webview application, which I write with Java. 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

    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

 @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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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