Xamarin WebView上的相机 [英] Camera on Xamarin WebView

查看:288
本文介绍了Xamarin WebView上的相机的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的Xamarin页面,其中包含一个调用WebRTC测试页面的WebView:

I have a simple Xamarin Page with a WebView that calls a WebRTC test page:

        _webView = new WebView
        {
            Source = "https://test.webrtc.org/",
            WidthRequest = 1000,
            HeightRequest = 1000
        };

        var stackLayout = new StackLayout()
        {
            Orientation = StackOrientation.Vertical,
            Padding = new Thickness(5, 20, 5, 10),
            Children = { _webView }
        };

        Content = new StackLayout { Children = { stackLayout } };

https://test.webrtc.org/页在同一Android上的Chrome上运行良好模拟器,但不能在WebView上显示"NotAllowedError".

The https://test.webrtc.org/ page works fine on Chrome on the same Android Emulator, but don't work on WebView saying "NotAllowedError".

该应用程序具有所需的权限.以下代码(使用Plugin.Permissions)返回true:

The application have the required permissions. The following code (that use Plugin.Permissions) returns true:

var statusCamera = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera);
var statusMicrophone = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Microphone);
return statusCamera == PermissionStatus.Granted && statusMicrophone == PermissionStatus.Granted;

怎么了?

谢谢

推荐答案

关于NotAllowedError,来自此处:

用户已指定不允许当前浏览实例访问设备;或者用户拒绝了当前会话的访问;或者用户已拒绝全局访问用户媒体设备.

The user has specified that the current browsing instance is not permitted access to the device; or the user has denied access for the current session; or the user has denied all access to user media devices globally.


您需要自定义WebView来覆盖WebChromeClientOnPermissionRequest方法.


You need custom a WebView to override the WebChromeClient's OnPermissionRequest method.

MyWebView类:

public class MyWebView: WebView
{
}

MyWebViewRendererMyWebClient类:

[assembly: ExportRenderer(typeof(App45.MyWebView), typeof(MyWebViewRenderer))]
namespace App45.Droid
{
    public class MyWebViewRenderer : WebViewRenderer
    {
        Activity mContext;
        public MyWebViewRenderer(Context context) : base(context)
        {
            this.mContext = context as Activity;
        }
            protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
        {
            base.OnElementChanged(e);
            Control.Settings.JavaScriptEnabled = true;
            Control.ClearCache(true);
            Control.SetWebChromeClient(new MyWebClient(mContext));
        }
        public class MyWebClient : WebChromeClient
        {
            Activity mContext;
            public MyWebClient(Activity context) {
                this.mContext = context;
            }
            [TargetApi(Value = 21)]
            public override void OnPermissionRequest(PermissionRequest request)
            {
                mContext.RunOnUiThread(() => {
                        request.Grant(request.GetResources());

                        });

            }
        }

    }

}

此处,我提供了一个演示供您测试.相机应该可以为您工作.

Here, I have provided a demo for you to test. The camera should work for you.

这篇关于Xamarin WebView上的相机的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆