Android Webview Permission denied error when try to video call - android

I am using a webview to load a url which has a feature to video call.
When i try to initiate a video call. I get the following error "Permission denied. Please refresh the page and allow access to your camera and microphone" .
If i give android runtime permission of CAMERA and RECORD_AUDIO. The video call will be initiated and the video will be transmitted to the computer also. But on the mobile side, I cannot view my video as well as the receiver's video. It shows the getDefaultVideoPoster() only.
I have already tried using WebChromeClient and WebViewClient but still no luck.
Here is my code:
private WebView webView;
webView = findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setSaveFormData(true);
webSettings.setSupportZoom(false);
webSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webView.setWebViewClient(new MyWebViewClient());
webView.setWebChromeClient(new WebChromeClient() {
#Override
public void onPermissionRequest(final PermissionRequest request) {
myRequest = request;
for (String permission : request.getResources()) {
switch (permission) {
case "android.webkit.resource.AUDIO_CAPTURE": {
askForPermission(request.getOrigin().toString(), Manifest.permission.RECORD_AUDIO, MY_PERMISSIONS_REQUEST_RECORD_AUDIO);
break;
}
case Manifest.permission.RECORD_AUDIO: {
askForPermission(request.getOrigin().toString(), Manifest.permission.RECORD_AUDIO, MY_PERMISSIONS_REQUEST_RECORD_AUDIO);
break;
}
case Manifest.permission.CAMERA: {
askForPermission(request.getOrigin().toString(), Manifest.permission.CAMERA, MY_PERMISSIONS_REQUEST_CAMERA);
break;
}
}
}
}
#Nullable
#Override
public Bitmap getDefaultVideoPoster() {
Log.i(TAG, "getDefaultVideoPoster");
return drawableToBitmap(getResources().getDrawable( R.drawable.ic_temperature));
//return super.getDefaultVideoPoster();
}
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
super.onShowCustomView(view, callback);
Log.i(TAG, "onShowCustomView");
if (view instanceof FrameLayout){
FrameLayout frame = (FrameLayout) view;
if (frame.getFocusedChild() instanceof VideoView){
VideoView video = (VideoView)
frame.getFocusedChild();
frame.removeView(video);
video.start();
}
}
}
});

webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
The above line code worked for me

Related

Android webview don`t display video

Is android webview able to display mp4 video with video tag?
In my application, my webview need to display video in . But, my webview is displays only blackscreen(audio is working fine).
I`m working in Android API Level 29.
This is my code about settings of webview
mWebSettings = mWebView.getSettings();
mWebSettings.setJavaScriptEnabled(true);
mWebSettings.setSupportMultipleWindows(true);
mWebSettings.setJavaScriptCanOpenWindowsAutomatically(true);
mWebSettings.setLoadWithOverviewMode(true);
mWebSettings.setUseWideViewPort(true);
mWebSettings.setSupportZoom(true);
mWebSettings.setBuiltInZoomControls(true);
mWebSettings.setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
mWebSettings.setCacheMode(WebSettings.LOAD_NO_CACHE);
mWebSettings.setDomStorageEnabled(true);
mWebSettings.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);
mWebSettings.setMediaPlaybackRequiresUserGesture(false);
mWebSettings.setPluginState(WebSettings.PluginState.ON);
mWebSettings.setAllowUniversalAccessFromFileURLs(true);
mWebSettings.setAllowFileAccessFromFileURLs(true);
mWebSettings.setAllowFileAccess(true);
mWebSettings.setUserAgentString("Mozilla/5.0 (Linux; Android 10; SM-A205U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.185 Mobile Safari/537.36");
mWebView.setBackgroundColor(0x00000000);
mWebView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
mWebView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
view.loadUrl(request.getUrl().toString());
}
return super.shouldOverrideUrlLoading(view, request);
}
});
mWebView.setWebChromeClient(new WebChromeClient() {
#SuppressLint("SetJavaScriptEnabled")
#Override
public boolean onCreateWindow(WebView view, boolean isDialog, boolean isUserGesture, Message resultMsg) {
// Dialog Create Code
WebView newWebView = new WebView(MainActivity.this);
WebSettings webSettings = newWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
newWebView.setVisibility(View.GONE);
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(newWebView);
ViewGroup.LayoutParams params = dialog.getWindow().getAttributes();
params.width = ViewGroup.LayoutParams.MATCH_PARENT;
params.height = ViewGroup.LayoutParams.MATCH_PARENT;
dialog.getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
//dialog.show();
newWebView.setWebChromeClient(new WebChromeClient() {
#Override
public void onCloseWindow(WebView window) {
dialog.dismiss();
}
});
// WebView Popup에서 내용이 안보이고 빈 화면만 보여 아래 코드 추가
newWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
return false;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
mWebView.loadUrl(view.getUrl());
}
});
((WebView.WebViewTransport) resultMsg.obj).setWebView(newWebView);
resultMsg.sendToTarget();
//Log.d(MainURL,newWebView.getUrl());
return true;
}
#SuppressLint("SetJavaScriptEnabled")
#Override
public void onCloseWindow(WebView window) {
super.onCloseWindow(window);
window.goBack();
}
});
And I use mWebView.loadUrl(url) to load my url.
mWebView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
This line is the cause of your problem. Video playback will not work when layer type is software. So change it either to LAYER_TYPE_NONE or LAYER_TYPE_HARDWARE like:
mWebView.setLayerType(WebView.LAYER_TYPE_HARDWARE , null);
or
mWebView.setLayerType(WebView.LAYER_TYPE_NONE, null);
Quick tip: Search more on internet regarding differences between various layer types and their performance and other aspects and then only use them wisely.

Android web view blank page

I searched for two days for a solution - including all stackover posts - and couldn't solve it ..
My web view can open sites like Google and Facebook but can't open the site in the code !
here is my full code
webView = findViewById(R.id.wv_pray_time);
webView.setWebChromeClient(new WebChromeClient());
webView.loadUrl("http://ramadanco.noursal.com/");
webView.setBackgroundColor(Color.TRANSPARENT);
WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setDomStorageEnabled(true);
webSettings.setDisplayZoomControls(false);
// No net error
webView.setWebViewClient(new WebViewClient() {
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
webView.loadUrl("file:///android_asset/error.html");
}
});
// Pull to refresh
mySwipeRefreshLayout = this.findViewById(R.id.swipeContainer);
mySwipeRefreshLayout.setOnRefreshListener(
new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
webView.reload();
if (mySwipeRefreshLayout.isRefreshing()) {
mySwipeRefreshLayout.setRefreshing(false);
}
}
}
);
}
#Override
public void onBackPressed() {
if (webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}

Ajax not working in android webview

I am loading a website in webview,we have used Ajax in website and its working fine on web browser and mobile browser also but in android webview ajax is not working no error is there in the console.Here is my code:-
public class Activity_WebView extends AppCompatActivity implements
ConnectivityReceiver.ConnectivityReceiverListener {
WebView webview;
ProgressDialog pro_dialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_view);
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setPluginState(WebSettings.PluginState.ON);
webview.setWebViewClient(new loadinsame());
pro_dialog = new ProgressDialog(Activity_WebView.this);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.getSettings().setAllowUniversalAccessFromFileURLs(true);
boolean connection = checkConnection();
if (connection) {
webview.loadUrl("website url");
} else {
Toast.makeText(Activity_WebView.this, "Sorry! Not connected to
internet", Toast.LENGTH_SHORT).show();
dialog_Show(webview, "Please check you Inernet connect and Reload.",
false);
}
}
#Override
public void onNetworkConnectionChanged(boolean isConnected) {
if (!isConnected) {
Toast.makeText(Activity_WebView.this, "Sorry! Not connected to
internet", Toast.LENGTH_SHORT).show();
}
}
private class loadinsame extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
pro_dialog.setCancelable(false);
pro_dialog.setMessage("Loading...");
pro_dialog.show();
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
pro_dialog.dismiss();
}
#Override
public void onReceivedError(final WebView webview, WebResourceRequest
request, WebResourceError error) {
super.onReceivedError(webview, request, error);
pro_dialog.dismiss();
// dialog_Show(webview, "Error Occur, Do you want to Reload?", true);
}
}
#Override
public void onBackPressed() {
if (webview.canGoBack()) {
webview.goBack();
} else {
super.onBackPressed();
}
}
private boolean checkConnection() {
boolean isConnected = ConnectivityReceiver.isConnected();
return isConnected;
}
#Override
protected void onResume() {
super.onResume();
MyApplication.getInstance().setConnectivityListener(this);
}
}
when i inspect the website in chrome using emulator, find that my ajax remain pending and then cancel after sometime.
Thanks in advance.
Just promoting #Jeff Thomas comment, by setting
mWebView.getSettings().setDomStorageEnabled(true);
worked!!
When i enabled the java script, its working.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.loadUrl("");
myWebView.getSettings().setDomStorageEnabled(true);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.setWebViewClient(new WebClient());
Solutions
If you want to load web url in WebView so you need follow my code It's code working fine and In web any type of script language used in web page.
public void webviewCallBack(String coverUrl) {
WebView WebView webView = (WebView) findViewById(R.id.web_view);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setAppCacheEnabled(true);
webView.getSettings().setDatabaseEnabled(true);
webView.getSettings().setDomStorageEnabled(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.clearView();
webView.measure(100, 100);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.setWebViewClient(new WebViewClient() {
// For api level bellow 24
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.d("weburl__", url);
if (url.startsWith("http") || url.startsWith("https")) {
// Return false means, web view will handle the link
return false;
} else if (url.startsWith("tel:")) {
// Handle the tel: link
handleTelLink(url);
// Return true means, leave the current web view and handle the url itself
return true;
}
return false;
}
// From api level 24
#RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
// Get the tel: url
String url = request.getUrl().toString();
Log.d("weburl_____", url);
if (url.startsWith("http") || url.startsWith("https")) {
// Return false means, web view will handle the link
return false;
} else if (url.startsWith("tel:")) {
// Handle the tel: link
handleTelLink(url);
// Return true means, leave the current web view and handle the url itself
return true;
}
return false;
}
});
// Load the url into web view
webView.loadUrl(coverUrl);
hoadler();
}
public void hoadler() {
Runnable task = new Runnable() {
public void run() {
material_design_progressbar.setVisibility(View.VISIBLE);
}
};
worker.schedule(task, 5, TimeUnit.SECONDS);
}
I hope this code is helpful for you!
Here is how to edit the AndroidManifes.xml:
Find AndroidManifest.xml file in application folder at:
android/app/src/main/AndroidManifest.xml
Locate the application subelement.
Add the following tag:
android:usesCleartextTraffic=”true”
The application subelement should now look like:
<application
android:name=”io.flutter.app.Test”
android:label=”bell_ui”
android:icon=”#`enter code
here`mapmap/ic_launcher”
android:usesCleartextTraffic=”true”>
Save the AndroidManifest.xml file.

Ignore ssl certificate requests in webview

I make small app which involve aspx pages , I try open it throw my webview I got blank screen .
I try open it with chrome browser it give me certificate needed
my code is very simple I add some documentation to explain what is going on
#SuppressLint("SetJavaScriptEnabled")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web);
Bundle bundle = getIntent().getExtras();
if(bundle != null){
urlSite = bundle.getString("passed_url");
}
_toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(_toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed();
}
});
webView = (WebView) findViewById(R.id.webView);
WebSettings webSettings = webView.getSettings();
// enable javascript
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setPluginState(WebSettings.PluginState.ON_DEMAND);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
new ParseURl().execute(new String[]{urlSite});
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
if(urlSite != null){
webView.clearCache(true);
webView.loadUrl(urlSite);
// webView.loadUrl("file:///android_asset/af.html");
}
}
After some research I found the answer, just override onReceivedSslError method and skip it.
#Override
public void onReceivedSslError(WebView view, SslErrorHandler handler, SslError error) {
handler.proceed(); // Ignore SSL certificate errors
}

Video plays only once in Webview of Android

I am succeeded to play streamed Youtube video from HTML5 content in Webview in Android but now problem is that video plays only first time. After that VideoView only goes to end of the video file.
I tried clearing cache as suggested here but no luck.
What could be the possible solution for this problem?
Please try following code to run Video, this has been using some suggestions given on stackoverflow.com
WebView webView = (WebView) findViewById(R.id.product_details_webview);
WebSettings webSettings = webView.getSettings();
webSettings.setPluginState(WebSettings.PluginState.ON);
webSettings.setJavaScriptEnabled(true);
webSettings.setUseWideViewPort(true);
webSettings.setLoadWithOverviewMode(true);
webView.setWebChromeClient(new chromeClient());
webView.setWebViewClient(new WebViewClient(){
});
webView.loadUrl(url);
public class chromeClient extends WebChromeClient implements OnCompletionListener,
OnErrorListener{
private WebView wv;
private VideoView mVideoView;
private LinearLayout mContentView;
private FrameLayout mCustomViewContainer;
private WebChromeClient.CustomViewCallback mCustomViewCallback;
FrameLayout.LayoutParams COVER_SCREEN_GRAVITY_CENTER = new
FrameLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER);
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
if (view instanceof FrameLayout) {
wv = (WebView)findViewById(R.id.product_details_webview);
mCustomViewContainer = (FrameLayout) view;
mCustomViewCallback = callback;
mContentView = (LinearLayout)findViewById(R.id.linearlayout1);
if (mCustomViewContainer.getFocusedChild() instanceof VideoView) {
mVideoView = (VideoView) mCustomViewContainer.getFocusedChild();
// frame.removeView(video);
mContentView.setVisibility(View.GONE);
mCustomViewContainer.setVisibility(View.VISIBLE);
setContentView(mCustomViewContainer);
mVideoView.setOnCompletionListener(this);
mVideoView.setOnErrorListener(this);
mVideoView.start();
}
}
}
public void onHideCustomView() {
if (mVideoView == null){
return;
}else{
// Hide the custom view.
mVideoView.setVisibility(View.GONE);
// Remove the custom view from its container.
mCustomViewContainer.removeView(mVideoView);
mVideoView = null;
mCustomViewContainer.setVisibility(View.GONE);
mCustomViewCallback.onCustomViewHidden();
// Show the content view.
mContentView.setVisibility(View.VISIBLE);
}
}
public void onCompletion(MediaPlayer mp) {
mp.stop();
mCustomViewContainer.setVisibility(View.GONE);
onHideCustomView();
setContentView(mContentView);
}
public boolean onError(MediaPlayer arg0, int arg1, int arg2) {
setContentView(mContentView);
return true;
}
}
Add in your chrome client :
#Override public void onPrepared(MediaPlayer mp) {
customViewCallback.onCustomViewHidden();
}
where customViewCallback = callback;
You got a video working inside a WebView with HTML5? Congrats! I failed trying to do that :(
To fix my issue I ended up leaving a place holder in the HTML and handling the video from native code using the MediaPlayer class.
I had this problem as well.
I had to add a seek(0).
Seems like the next video played from exactly where the last video ended.
So I had to go to the beginning of the video before playing.

Categories

Resources