简体   繁体   中英

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

I am displaying the groupword API in webview. The webview is not able to connect to my phones microphone and speaker how ever i allowed all permissions to the application.

    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

  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

in your Android project .

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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