gpt4 book ai didi

java - Xamarin.Forms WebView 无法与 WebRTC 配合使用

转载 作者:行者123 更新时间:2023-12-01 16:20:34 26 4
gpt4 key购买 nike

我曾参与过 WebRTC 项目,它可以在 Web 浏览器上运行,但当我放入应用程序 WebView 时,它显示未授权访问设备类型的消息。

我阅读了有关根据权限请求授予访问权限的其他解决方案,但对我来说似乎还不太幸运;仍然无法访问相机/麦克风

这是我的一些实现(C# 语言):

...
Control.SetWebChromeClient(new WebChromeClient(context))
...
//on WebChromeClient class
public override void OnPermissionRequest(PermissionRequest request)
{
mContext.RunOnUiThread(() =>
{
request.Grant(request.GetResources());

});
}

在 Java Android 中完成相同的实现,相同的结果未授权。测试设备运行 Android 9 和 Android 10。

使用的其他 Android 权限:

<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 上,您可以使用权限插件。 https://github.com/jamesmontemagno/PermissionsPlugin

在 list 文件中添加权限:

<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

关于java - Xamarin.Forms WebView 无法与 WebRTC 配合使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62295556/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com