Xamarin.Forms WebView 不适用于 WebRTC
Posted
技术标签:
【中文标题】Xamarin.Forms WebView 不适用于 WebRTC【英文标题】:Xamarin.Forms WebView not working with WebRTC 【发布时间】:2020-09-29 09:29:03 【问题描述】:我在 WebRTC 项目上工作过,它都在 Web 浏览器上工作,但是当我输入 Application WebView 时,它说not authorized to access device
kind 一条消息。
我阅读了其他关于根据权限请求授予访问权限的解决方案,但我似乎还没有运气;仍然无法访问相机/麦克风
这是我的一些实现(C# 语言):
...
Control.SetWebChromeClient(new WebChromeClient(context))
...
//on WebChromeClient class
public override void OnPermissionRequest(PermissionRequest request)
mContext.RunOnUiThread(() =>
request.Grant(request.GetResources());
);
在 Java android 中完成相同的实现,相同的结果 not authorized
。测试设备运行 Android 9 和 Android 10。
使用的其他安卓权限:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAPTURE_AUDIO_OUTPUT" />
<uses-permission android:name="android.permission.CAPTURE_SECURE_VIDEO_OUTPUT" />
<uses-permission android:name="android.permission.CAPTURE_VIDEO_OUTPUT" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
Java 或 C# 上的任何实现都可接受。希望大家给点建议。
已编辑
根据@WendyZang-MSFT 建议使用PermissionPlugin
,下面是代码。
//The Utils class
public static class Utils
public static async Task<PermissionStatus> CheckPermissions(Permission permission)
var permissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
bool request = false;
if (permissionStatus == PermissionStatus.Denied)
if (Device.RuntimePlatform == Device.ios)
var title = $"permission Permission";
var question = $"To use this plugin the permission permission is required. Please go into Settings and turn on permission for the app.";
var positive = "Settings";
var negative = "Maybe Later";
var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
if (task == null)
return permissionStatus;
var result = await task;
if (result)
CrossPermissions.Current.OpenAppSettings();
return permissionStatus;
request = true;
if (request || permissionStatus != PermissionStatus.Granted)
var newStatus = await CrossPermissions.Current.RequestPermissionsAsync(permission);
if (!newStatus.ContainsKey(permission))
return permissionStatus;
permissionStatus = newStatus[permission];
if (newStatus[permission] != PermissionStatus.Granted)
permissionStatus = newStatus[permission];
var title = $"permission Permission";
var question = $"To use the plugin the permission permission is required.";
var positive = "Settings";
var negative = "Maybe Later";
var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
if (task == null)
return permissionStatus;
var result = await task;
if (result)
CrossPermissions.Current.OpenAppSettings();
return permissionStatus;
return permissionStatus;
这是我的运行时权限:
protected override void OnAppearing()
base.OnAppearing();
RunTimePermission();
public async void RunTimePermission()
var status = PermissionStatus.Unknown;
status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera);
if (status != PermissionStatus.Granted)
status = await Utils.CheckPermissions(Permission.Camera);
await Utils.CheckPermissions(Permission.Microphone);
status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Microphone);
if (status != PermissionStatus.Granted)
status = await Utils.CheckPermissions(Permission.Microphone);
在PermissionPlugin
上运行似乎不错,因为它在加载网页时询问权限,但仍然出现错误提示:
Requires MODIFY_AUDIO_SETTINGS and RECORD_AUDIO. No audio device will be available for recording
【问题讨论】:
在 Xamarin.Forms 上,您可以使用权限插件来请求权限。 github.com/jamesmontemagno/PermissionsPlugin 这是我之前做过的关于如何请求相机权限的代码示例。 ***.com/questions/59771557/… @WendyZang-MSFT 我以前读过你的帖子,我已经试过了,但还是不行。我需要把我的代码放在这里吗? 是的,更多代码会有所帮助。 日志似乎没有说任何关于相机的信息,但仍然说关于麦克风的错误。所以我添加了Permission.Microphone
,我不知道这样做是否正确
xamarin 项目运行良好吗?我不知道WebRTC
。
【参考方案1】:
在 Xamarin.Forms 上,您可以使用权限插件。 https://github.com/jamesmontemagno/PermissionsPlugin
在 Manifest 文件中添加权限:
<uses-permission android:name="android.permission.CAMERA" />
创建一个 Utils.cs
public static class Utils
public static async Task<PermissionStatus> CheckPermissions(Permission permission)
var permissionStatus = await CrossPermissions.Current.CheckPermissionStatusAsync(permission);
bool request = false;
if (permissionStatus == PermissionStatus.Denied)
if (Device.RuntimePlatform == Device.iOS)
var title = $"permission Permission";
var question = $"To use this plugin the permission permission is required. Please go into Settings and turn on permission for the app.";
var positive = "Settings";
var negative = "Maybe Later";
var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
if (task == null)
return permissionStatus;
var result = await task;
if (result)
CrossPermissions.Current.OpenAppSettings();
return permissionStatus;
request = true;
if (request || permissionStatus != PermissionStatus.Granted)
var newStatus = await CrossPermissions.Current.RequestPermissionsAsync(permission);
if (!newStatus.ContainsKey(permission))
return permissionStatus;
permissionStatus = newStatus[permission];
if (newStatus[permission] != PermissionStatus.Granted)
permissionStatus = newStatus[permission];
var title = $"permission Permission";
var question = $"To use the plugin the permission permission is required.";
var positive = "Settings";
var negative = "Maybe Later";
var task = Application.Current?.MainPage?.DisplayAlert(title, question, positive, negative);
if (task == null)
return permissionStatus;
var result = await task;
if (result)
CrossPermissions.Current.OpenAppSettings();
return permissionStatus;
return permissionStatus;
用法:
var status = PermissionStatus.Unknown;
status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Camera);
if (status != PermissionStatus.Granted)
status = await Utils.CheckPermissions(Permission.Camera);
这是我之前完成的关于如何请求相机权限的代码示例。 Xamarin - Requesting camera permissions in WebView
【讨论】:
@Wendy-Zhang,您的解决方案似乎仅适用于Android,iOS webview仍然无法访问相机和麦克风。有什么建议吗? IOS权限可以查看以下链接。 docs.microsoft.com/en-us/xamarin/ios/app-fundamentals/… 您提供的链接是为了授予该应用程序的权限。但它还不起作用。【参考方案2】:我找到了解决方案并遇到了其他问题。基于@Wendy-Zhang 对Xamarin - Requesting camera permissions in WebView 的回答,如果遵循单一解决方案,它对我不起作用,相反,我把这两个解决方案放在一起,这就是诀窍。
【讨论】:
以上是关于Xamarin.Forms WebView 不适用于 WebRTC的主要内容,如果未能解决你的问题,请参考以下文章
Xamarin.Forms - 如果 webView 包含在 Android 上打开的模式或菜单,有没有办法禁用拉刷新?
Xamarin.Forms (UWP) - 如何获取 WebView 的 DOM 作为 HTML 字符串?
Xamarin.Forms中为WebView指定数据来源Source
Xamarin.Forms自定义用户界面控件实现一个HybridWebView(混合webview)