I want to use getUserMedia in Android Lolipop 5.0 WebView

Asked 2 years ago, Updated 2 years ago, 53 views

I would like to use getUserMedia to start the webcam on Android WebView.
The following code is written, but errorCallback is called.

Please tell me the solution.

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

// Settings
WebSettings webSettings=myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setAllowFileAccessFromFileURLs(true);
webSettings.setAllowUniversalAccessFromFileURLs(true);
myWebView.addJavascriptInterface(newWebAppInterface(this), "Android");

// Set a web view client and a chrome client
myWebView.setWebViewClient(newWebViewClient());
myWebView.setWebChromeClient(newWebChromeClient(){
  // Need to accept permissions to use the camera and audio
  @ Override
  public void onPermissionRequest (final PermissionRequest) {
    Log.d(TAG, "onPermissionRequest";
    MainActivity.this.runOnUiThread(newRunnable(){
      @TargetApi (Build.VERSION_CODES.LOLIPOP)
      @ 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);

XML

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    tools:context=".MainActivity">
    <WebView android:layout_width="fill_parent"
         android:layout_height="fill_parent"
         android: id="@+id/webview"/>
</RelativeLayout>

[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 constructs={video:true, audio:true};
 var localStream=null;
 function init() {
    console.log('Getting user media with constructs', constructs);
    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);
    // [object NavigatorUserMediaError]
 };

 window.onload=function(){
    init();
 };

Note: The alert output was incorrect.

The value obtained by errorCallback is oobject NavigatorUserMediaError. に
The name of the object is "PermissionDeniedError".

android

2022-09-30 20:51

1 Answers

Implementing WebChromeClient.onPermissionRequest should help.


2022-09-30 20:51

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.