简体   繁体   English

在Xamarin表单的Web视图中访问麦克风时出现错误?

[英]error while accessing the microphones using in web view in xamarin forms?

I am displaying the groupword API in webview. 我在webview中显示groupword API。 The webview is not able to connect to my phones microphone and speaker how ever i allowed all permissions to the application. Webview无法将我的手机麦克风和扬声器连接到我的应用程序的所有权限。

    async Task<bool> RequestPermission()
    {
        var status = PermissionStatus.Unknown;
        status = await CrossPermissions.Current.CheckPermissionStatusAsync(Permission.Microphone);
        if (status != PermissionStatus.Granted)
        {
            if (await CrossPermissions.Current.ShouldShowRequestPermissionRationaleAsync(Permission.Microphone))
            {
                //await DisplayAlert("Need Microphone", "We need microphone permission", "OK");
                return false;
            }

            var results = await CrossPermissions.Current.RequestPermissionsAsync(Permission.Microphone);
            status = results[Permission.Microphone];
            return true;

        }
        if (status == PermissionStatus.Granted)
        {
            return true;
        }
        return false;
    }

Calling WebView 调用WebView

  var Status = await RequestPermission();
        if(Status)
        {
            await Navigation.PushAsync(new InAppBrowserXaml("https://www.groupworld.net/room/1/demo?hide_playback=true"));


        }
        else
        {
            await DisplayAlert("Need Microphone", "We need microphone permission", "OK");
            return;
        }

You need to grant the permission by using CustomRenderer 您需要使用CustomRenderer授予权限

in your Android project . 在您的Android项目中。

using Android.App;
using Android.Content;
using Android.Net.Http;
using Android.OS;
using Android.Runtime;
using Android.Views;
using Android.Webkit;
using Android.Widget;
using xxx;
using xxx.Droid;
using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

[assembly: ExportRenderer(typeof(Xamarin.Forms.WebView), typeof(CustomWebViewRenderer))]
namespace xxx.Droid
{

    public class MyWebChromeClient : WebChromeClient
    {

        public override void OnPermissionRequest(PermissionRequest request)
        {
            base.OnPermissionRequest(request);

            request.Grant(request.GetResources());

        }
    }


    public class CustomWebViewRenderer : WebViewRenderer
    {

        public CustomWebViewRenderer(Context context) : base(context)
        {

        }

        protected override void OnElementChanged(ElementChangedEventArgs<Xamarin.Forms.WebView> e)
        {
            base.OnElementChanged(e);

            if (e.NewElement != null)
            {
                var customWebView = Element as Xamarin.Forms.WebView;
               // Control.SetBackgroundColor (Android.Graphics.Color.Red);

                Control.Settings.JavaScriptEnabled = true;
                Control.SetWebChromeClient(new MyWebChromeClient());
            }
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM