Xamarin Android Webview Camera Permission granted but no access

Viewed 462

I have created a webview app, where I scan QR Code. Everything works fine on mobile and desktop browser, but in the app I get Unable to access video stream (please make sure you have a webcam enabled).

I am sharing some of the Code below :

Here is the HTML which I am using to scan QR Code :

<div id="loadingMessage">
       Unable to access video stream (please make sure you have a webcam enabled)
</div>
<canvas id="canvas" hidden></canvas>
<div id="output" style="padding-right: 35px;padding-left: 35px;width: fit-content;">
  <div><b> QR Code Data:</b> <span id="outputData"></span></div>
</div>

And the JS :

<script>

var video = document.createElement("video");
var canvasElement = document.getElementById("canvas");
var canvas = canvasElement.getContext("2d");
var loadingMessage = document.getElementById("loadingMessage");
var outputContainer = document.getElementById("output");
var outputData = document.getElementById("outputData");

function drawLine(begin, end, color) {
    canvas.beginPath();
    canvas.moveTo(begin.x, begin.y);
    canvas.lineTo(end.x, end.y);
    canvas.lineWidth = 4;
    canvas.strokeStyle = color;
    canvas.stroke();
}

navigator.mediaDevices.getUserMedia({ video: { facingMode: "user" } }).then(function (stream) {
    video.srcObject = stream;
    video.setAttribute("playsinline", true); 
    video.play();
    requestAnimationFrame(tick);
});

function tick() {
    loadingMessage.innerText = "⌛ Loading video..."
    if (video.readyState === video.HAVE_ENOUGH_DATA) {
        loadingMessage.hidden = true;
        canvasElement.hidden = false;
        outputContainer.hidden = false;

        canvasElement.height = video.videoHeight;
        canvasElement.width = video.videoWidth;
        canvas.drawImage(video, 0, 0, canvasElement.width, canvasElement.height);
        var imageData = canvas.getImageData(0, 0, canvasElement.width, canvasElement.height);
        var code = jsQR(imageData.data, imageData.width, imageData.height, {
            inversionAttempts: "dontInvert",
        });
        if (code) {
            drawLine(code.location.topLeftCorner, code.location.topRightCorner, "#FF3B58");
            drawLine(code.location.topRightCorner, code.location.bottomRightCorner, "#FF3B58");
            drawLine(code.location.bottomRightCorner, code.location.bottomLeftCorner, "#FF3B58");
            drawLine(code.location.bottomLeftCorner, code.location.topLeftCorner, "#FF3B58");
            outputData.innerText = code.data;
        }
    }
    requestAnimationFrame(tick);
}

</script>

I am also asking permission to grant Camera Access at Runtime in MainActivity.cs.

Here is the code related to Permission in Xamarin Webview :

class MyWebClient : WebChromeClient
{
    Activity mContext;

    
    public MyWebClient(Activity context)
    {
        this.mContext = context;
    }
    public override void OnPermissionRequest(PermissionRequest request)
    {
        mContext.RunOnUiThread(() => {
            request.Grant(request.GetResources());
        });
    }
}

I have also used the following permissions in Manifest file :

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

I also tried the solution suggested here and here but it didn't work for me.

1 Answers

Adding MediaPlaybackRequiresUserGesture = false is what did it for me (testing with webrtc). Try it out. I added a portion of my renderer. LMK if you need it all.

 protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
    {
        base.OnElementChanged(e);
        Control.Settings.JavaScriptEnabled = true;
        Control.Settings.MediaPlaybackRequiresUserGesture = false;
        Control.ClearCache(true);
        Control.SetWebChromeClient(new MyWebClient(mContext));
    }
Related