mgks / Android-SmartWebView

A simple use webview integrated w/ native features to help create most advanced hybrid applications.
https://mgks.dev/smart-webview
MIT License
561 stars 278 forks source link

HTML5 video not working: permission denied #250

Open esiokugbechoice opened 2 years ago

esiokugbechoice commented 2 years ago

I am using an HTML5 QR code reader https://github.com/mebjas/html5-qrcode. everything works fine on chrome, but I get the following error when i try from the webview


Please help...

husaindevelop commented 2 years ago

The camera's permission is disabled, that's why you are getting this error. Simply follow the code given below, the camera will start working as well as your scanner which depends on it.

Under MainActivity.Java file / asw_view.SetwebChromeClient. Post this code

@Nullable
            public Bitmap getDefaultVideoPoster() {
                return Bitmap.createBitmap(10, 10, Bitmap.Config.ARGB_8888);
            }
            @Override
            public void onPermissionRequest(@NotNull final PermissionRequest request) {
                                final String[] requestedResources = request.getResources();
                for (String r : requestedResources) {
                    if (r.equals(PermissionRequest.RESOURCE_VIDEO_CAPTURE)) {
                        request.grant(new String[]{PermissionRequest.RESOURCE_VIDEO_CAPTURE});
                        break;
                    }
                    if (r.equals(PermissionRequest.RESOURCE_AUDIO_CAPTURE)) {
                        request.grant(new String[] { PermissionRequest.RESOURCE_AUDIO_CAPTURE});
                        break;
                    }
                }
            }

Add the line under websettings webSettings.setMediaPlaybackRequiresUserGesture(false);

And make sure record_audio and camera permissions are granted in android manifest xml.

Macof08 commented 1 year ago

i got the same problem and @husaindevelop, i put your code but give me error when i build app.

error: cannot find symbol public void onPermissionRequest(@NotNull final PermissionRequest request) { ^ symbol: class PermissionRequest

@mgks any idea?

frogsuite commented 7 months ago

replace @NotNull with @Nullable also import import androidx.annotation.NonNull; import org.jetbrains.annotations.Nullable;