Getting Access to Camera via Webview in Xamarin , Android, Webrtc

Vuyiswa Maseko 351 Reputation points
2022-10-07T23:10:18.67+00:00

Good Day All

i have a web page that i am accessing via Webview in Xamarin , Test URL

https://www.trovadating.com/Webview/GoLive.aspx?user_id=10383

and in my Android permission are as follows

<uses-permission android:name="android.permission.CAMERA" />  
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />  
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />  
<uses-permission android:name="android.permission.INTERNET" />   
<uses-permission android:name="android.permission.RECORD_AUDIO" />  
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />   
<uses-permission android:name="android.permission.MICROPHONE" />   
   <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" />  

 

but i am getting an error on my webrtc function that the camera is in use or it cant be used

connection.onMediaError = function(error, constraints) {  
        if (!!connection.enableLogs)  
        {  
            if (error == "Starting videoinput failed") {  
                alert("Your Camera is Already being used");  
            }  
            else  
            {  
                alert("Your Camera is Already being used");  
                console.error(error, constraints);  
            }  
        }  
    }  

Thanks

Developer technologies .NET Xamarin
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2022-10-10T09:51:46.14+00:00

    Hello,

    Do you request these permissions when you application is running. If not, please add request permission code in your OnCreate method of MainActivity like following code. I use your test url, it is working in my side.

       public static MainActivity Instance;  
         
        protected async override void OnCreate(Bundle savedInstanceState)  
               {  
                   base.OnCreate(savedInstanceState);  
                   Instance = this;  
                   Xamarin.Essentials.Platform.Init(this, savedInstanceState);  
                   global::Xamarin.Forms.Forms.Init(this, savedInstanceState);  
         
         
         
                   
                   if (ContextCompat.CheckSelfPermission(MainActivity.Instance, Manifest.Permission.Camera) != Permission.Granted)  
                   {  
         
         
         
                      await Permissions.RequestAsync<Permissions.Camera>();  
                       await Permissions.RequestAsync<Permissions.Microphone>();  
                       await Permissions.RequestAsync<Permissions.Media>();  
         
         
         
                  }  
                   LoadApplication(new App());  
               }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Vuyiswa Maseko 351 Reputation points
    2022-10-10T13:03:59.543+00:00

    Good Day

    i resolved the problem

    on Page load where i want the streaming to happen i added this

     private async Task<bool> initPer()  
            {  
                var statusCamera = await Permissions.RequestAsync<Permissions.Camera>();  
                var statusMicrophone = await Permissions.RequestAsync<Permissions.Microphone>();  
                return statusCamera == PermissionStatus.Granted && statusMicrophone == PermissionStatus.Granted;  
            }  
    

    and i created a renderer on the platfirm like this

    [assembly: ExportRenderer(typeof(myapp.MyWebView), typeof(MyWebViewRenderer))]  
    namespace myapp.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());  
                         
                            });  
                      
                }  
            }  
             
        }  
    

    and used it like this

            <local1:MyWebView  
                x:Name="webLive"    
           HorizontalOptions="FillAndExpand" VerticalOptions="FillAndExpand"/>  
    

    and also

    public class MyWebView: WebView  
    {  
    }  
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.