Xamarin WebView上的相机(Camera on Xamarin WebView)

编程入门 行业动态 更新时间:2024-10-10 13:20:19
Xamarin WebView上的相机(Camera on Xamarin WebView)

我有一个带WebView的简单Xamarin页面,它调用WebRTC测试页面:

_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”。

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

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

怎么了?

谢谢

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 } };

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

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;

What's wrong?

Thanks

最满意答案

关于NotAllowedError ,从这里开始 :

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


您需要自定义WebView来覆盖WebChromeClient的OnPermissionRequest方法。

PCL中的MyWebView类:

public class MyWebView: WebView { }

MyWebViewRenderer和MyWebClient类:

[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()); }); } } } }

在这里 ,我提供了一个演示供您测试。 相机应该为你工作。

About NotAllowedError, from here:

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.


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

MyWebView class in PCL:

public class MyWebView: WebView { }

MyWebViewRenderer and MyWebClient class:

[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.

更多推荐

WebView,new,var,The,test,电脑培训,计算机培训,IT培训"/> <meta name="desc

本文发布于:2023-07-06 07:44:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1047460.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:相机   Xamarin   WebView   Camera

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!