Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I want to use the getUserMedia in Android Lollipop 5.0 WebView

Tags:

android

I want to start a web camera using the getUserMedia in WebView of Android. The following has written the code, but, errorCallback will have is called. why not?

Incidentally, The following web site was implemented by reference to.

Android, WebViews, & getUserMedia: Putting it all together

myWebView = (WebView) findViewById(R.id.webview);

// Settings
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
myWebView.addJavascriptInterface(new WebAppInterface(this), “Android”);

// Set a web view client and a chrome client
myWebView.setWebViewClient(new WebViewClient());
myWebView.setWebChromeClient(new WebChromeClient() {
  // Need to accept permissions to use the camera and audio
  @Override
  public void onPermissionRequest(final PermissionRequest request) {
    Log.d(TAG, “onPermissionRequest”);
    MainActivity.this.runOnUiThread(new Runnable() {
      @TargetApi(Build.VERSION_CODES.LOLLIPOP)
      @Override
      public void run() {
        if(request.getOrigin().toString().equals(LOCAL_FILE)) {
          request.grant(request.getResources());
        } 
        else {
            request.deny();
        }
      }
    });
  }
});
// Load the local HTML file into the webview
myWebView.loadUrl(LOCAL_FILE);

[permissions]

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.MODIFY_AUDIO_SETTINGS" />

[JavaScript]

 var constraints = {video: true, audio: true};
 var localStream = null;
 function init() {
    console.log(‘Getting user media with constraints’, constraints);
    navigator.webkitGetUserMedia(constraints, handleUserMediaSuccess, handleUserMediaError);
 };

 function handleUserMediaSuccess(stream) {
    console.log(‘[+] Successfully got local video stream!’);
    attachMediaStream = function(element, stream) {
          element.src = window.webkitURL.createObjectURL(stream);
    };
    attachMediaStream(document.getElementById(“localVideo”), stream);
 };

 function handleUserMediaError(error) {
    alert(‘[!] getUserMedia error: ‘, error);
 };

 window.onload = function() {
    init();
 };
like image 972
user3380237 Avatar asked Jun 16 '15 00:06

user3380237


1 Answers

i fixed the same problem with this code

 mWebView.setWebChromeClient(new WebChromeClient() {
        @Override
        public void onPermissionRequest(PermissionRequest request) {
            request.grant(request.getResources());
        }
    });
like image 122
Hudson Moreira da Cunha Avatar answered Sep 21 '22 19:09

Hudson Moreira da Cunha