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();
};
i fixed the same problem with this code
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onPermissionRequest(PermissionRequest request) {
request.grant(request.getResources());
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With