Does getUserMedia works in ionic-webview in Android app?

断了今生、忘了曾经 提交于 2019-12-20 05:38:06

问题


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" />

回答1:


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:

  • Install Ionic
  • ionic start getUserMedia blank
  • cd getUserMedia
  • ionic cordova plugin add cordova-plugin-android-permissions
  • npm install --save @ionic-native/android-permissions
  • npm install webrtc-adapter --save
  • link to adapter in angular.json scripts (link)
  • Add AndroidPermissions to app.module.ts provider (link)
  • Create camera access code in home.component.ts (link)
  • Add android manifiest permissions to config.xml and AndroidManifest.xml (link, if the config doesn't copy go to platforms/android/app/src/AndroidManifest.xml)
  • ionic cordova platform add android
  • ionic cordova build android
  • ionic cordova run android

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:

  • CORS of the embedded site need to have the allowed access origin header
  • enable security permissions to access the camera and microphone <iframe allow="camera; microphone">

Please read the following for more info on iframe features:

  • https://www.chromestatus.com/feature/5023919287304192
  • https://bugs.webkit.org/show_bug.cgi?id=167430
  • https://dev.chromium.org/Home/chromium-security/deprecating-permissions-in-cross-origin-iframes
  • https://bugzilla.mozilla.org/show_bug.cgi?id=1389198
  • https://github.com/instructure/canvas-lms/issues/1219

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:

  • Why CordovaWebViewClient not working in Cordova 6 anymore
  • https://github.com/marcusbelcher/rn-getUserMedia-test
  • https://github.com/marcusbelcher/android-getUserMedia-test
  • Cordova version of this question: NotReadableError: Could not start source
  • Progressive Web App: Error Accessing navigator.mediaDevices.getUserMedia?



回答2:


In Android Chrome Browser. Navigator.mediaDevice.getUserMedia() will support only from Chrome Version 52. Please check your chrome browser version.

You can use Cordova-plugin-media cordova plugin for recording Audio Files.



来源:https://stackoverflow.com/questions/52557767/does-getusermedia-works-in-ionic-webview-in-android-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!