androidionic-frameworkgetusermediaionic-webview

Does getUserMedia works in ionic-webview in Android app?


I have given all the permissions for camera and microphone. Enable CSP in meta tag for the iframe. Still not able to get permission for camera and microphone in getusermedia for iframe.

home.html

ion-content class= 'padding has-subheader'>

app-component.ts

constructor(platform: Platform, statusBar: StatusBar, splashScreen: SplashScreen, private androidPermissions: AndroidPermissions) 
{
platform.ready().then
   (() => 
      {

           statusBar.styleDefault();
           splashScreen.hide();

           this.androidPermissions.requestPermissions([
                this.androidPermissions.PERMISSION.CAMERA, 
                this.androidPermissions.PERMISSION.MODIFY_AUDIO_SETTINGS,
            this.androidPermissions.PERMISSION.RECORD_AUDIO
           ]);

  //navigator.mediaDevices
  navigator.mediaDevices
    .getUserMedia({
      audio: true,
      video: true
    })
    .then(mediaStream => {
        console.log("Video camera platform ready")
    });


});
}

AndroidManifest.xml

 <uses-sdk android:minSdkVersion="23" android:targetSdkVersion="26" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />
    <uses-feature android:name="android.hardware.camera" />
    <uses-feature android:name="android.hardware.camera.autofocus" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

Solution

  • Update - 04/11/2018 - Working Ionic example

    As promised I had a crack at this today. To answer your question now in full: Yes you can access getUserMedia inside of ionic on Android.

    See my GitHub project here for a working example and for screenshots.

    See this feature branch here which successfully tests getUserMedia inside of an iframe

    Steps involved:

    You can also see alternative versions of this for React Native, native Android and Cordova.

    As of this update support for iOS is still a no due to Apple security restrictions on WKWebView and UIWebView.

    Iframes: To make sure getUserMedia works inside of them make sure you:

    Please read the following for more info on iframe features:


    Yes, it theoretically can work. See my example below on how to do it in Android. For iOS this isn't currently possible due to the limitations of WKWebView. I have linked to a StackOverFlow question below on how people are achieving this on Cordova which Ionic is based off.

    The core main steps which need to be achieved to get getUserMedia working on Android regardless of framework are:

    1. Make sure your app is Android API 21 and above
    2. Add permissions to Manifest (link to file)
    3. Make sure you use getUserMedia adapter for API compatibility
    4. Make sure you add webrtc to the Android project gradle file (link to file)
    5. Override Chrome WebView permissions to grand access (link to file example, line 106)
    6. On app start ask user for permission to access the camera.

    The issue you are specifically facing is 4. or 5. It looks like the project gets you that far. The permission error is most likely down to the Chrome WebView your app is using doesn't override the permission, and/or it is and the user hasn't manually enabled the permission.

    Thus within the Ionic framework you need to extend its Browser to get access to the override onRequestPermissionsResult. To do this you need to make a Cordova plugin and then create the Ionic bindings.

    More reading about this for similar frameworks can be found here: