html5 in a WebView, detect when video is started - android

In a WebView, I'm trying to load a html5 page where I embed a <video> but is there any way I can detect when the user selects the play button or the video starts playing?
Right now is when I play the video, default is it fires an Intentto open Android's default player, I don't want it to fire an Intent, I want to use a Dialog and play it there. If anybody can give an idea. Thanks.

#Override
public void onCreate(Bundle savedInstanceState) {
....
mywebviewer = (WebView) findViewById(R.id.mywebviewer);
WebSettings webSettings = mywebviewer.getSettings();
webSettings.setJavaScriptEnabled(true);
mywebviewer.loadUrl("https://www.youtube.com");
mywebviewer.setWebViewClient(new WebViewClient());
}
#Override
public void onBackPressed() {
if (mywebviewer.canGoBack()) {
mywebviewer.goBack();
} else {
super.onBackPressed();
}
}

I do it in this way:
boolean videoPlay=false;
webView.setWebChromeClient(new WebChromeClient() {
#Override
public Bitmap getDefaultVideoPoster() {
super.getDefaultVideoPoster();
videoPlay=true;
Log.d("vplayyyyyyyy",String.valueOf(videoPlay));
return null;
}
});
hope this help :)

Related

My webview is opening the webpage in the default browser

So I just implemented a simple webview application in which i was loading the stackoverflow main page. Earlier it was working just fine but now as I click on some link it opens that link in the default browser. I have implemented and override the shouldoverrideUrlLoading method by creating my custom webViewClient class.
I know that there are various question ask like these but I am writing this question only because they don't work for me.
public class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if(Uri.parse(url).getHost().endsWith(".com"))
return false;
Intent intent = new Intent(Intent.ACTION_VIEW,Uri.parse(url));
view.getContext().startActivity(intent);
return true;
}
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webView = findViewById(R.id.webview);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new MyWebViewClient());
final customEditText editText = findViewById(R.id.urlEditText);
Button button = findViewById(R.id.enterButtonId);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
webView.loadUrl("https://"+editText.getText().toString().trim().toLowerCase());
}
});
}
You can use this:
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("url");
Just implement the web client and set it before loadUrl. The simplest way is:
WebView.setWebViewClient(new WebViewClient());
So When I was reading a tutorial https://www.journaldev.com/9333/android-webview-example-tutorial in this it is given that when shouldOverrideUrlLoading( ) method provides false then it url opens in our webview and if it returns true, then it will not load the page at all. So I think that earlier my code was working was because I was opening .com extensioned websites but when i open other extension website then it redirect to the default browser.

Android - when exiting a WebView, the audio still keeps playing

I have this code which works and the only problem with it is that if I leave the WebView and even close the app, and even press power on the phone, the audio from the podcasts keeps playing.
Would anyone know how to stop that?
Here is the code:
public class PodcastsActivity extends BaseActivity //implements ActionBar.OnNavigationListener
{
WebView webview = null;
#Override
public void onCreate(Bundle savedInstanceState)
{
//setTheme(R.style.Theme_Sherlock_Light);
super.onCreate(savedInstanceState);
//setContentView(R.layout.podcasts);
webview = new WebView(this);
webview.getSettings().setAppCacheEnabled(false);
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(1);
webview.getSettings().setPluginState(PluginState.ON);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
//webSettings.setBuiltInZoomControls(true);
WebSettings webSettings = webview.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
//webSettings.getMediaPlaybackRequiresUserGesture();
webSettings.setAllowContentAccess(true);
webSettings.setEnableSmoothTransition(true);
webSettings.setLoadsImagesAutomatically(true);
webSettings.setLoadWithOverviewMode(true);
webSettings.setSupportZoom(true);
webSettings.setUseWideViewPort(true);
setContentView(webview);
webview.loadUrl("http://www.stitcher.com/podcast/entrepreneuronfirecom/entrepreneur-on-fire-tim-ferriss-other-incredible-entrepreneurs");
}
// public void onBackPressed ( )
// {
// Class.forName("com.***.HTML5WebView").getMethod("onPause", (Class[]) null).
// invoke(html5WebView, (Object[]) null);
// }
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack())
{
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onStop()
{
super.onStop();
// your code
webview.clearView();
}
You should call through to the WebView's onPause() and onResume() from your Activity's onPause() and onResume(), respectively.
Pauses any extra processing associated with this WebView and its
associated DOM, plugins, JavaScript etc. For example, if this WebView
is taken offscreen, this could be called to reduce unnecessary CPU or
network traffic. When this WebView is again "active", call onResume().
There's also pauseTimers(), which affects all of the WebViews within your application:
Pauses all layout, parsing, and JavaScript timers for all WebViews.
This is a global requests, not restricted to just this WebView. This
can be useful if the application has been paused.
Accepted answer didnt work for me - just using onPause in a fragment was not enough to stop music on Android 9. I had to also do:
override fun onDestroyView() {
webview?.destroy()
super.onDestroyView()
}
Same issue for the Jetpack Compose can be solved by capturing the WebView (https://google.github.io/accompanist/webview/) in "onCreated" and then use it to call "destroy()"
val webViewState = rememberWebViewState(webViewUrl.value)
val webView = remember { mutableStateOf<android.webkit.WebView?>(null) }
//..inside Composable..
WebView(state = webViewState,
captureBackPresses = true,
onCreated = { wv ->
webView.value = wv
}
)
//..on Back Press/Close Button..
webView.value?.destroy()
In my case WebView onPause() and onResume() is not working for me.
Solution:
onPause I load the below URL to stop the audio:
webView.loadUrl("javascript:document.location=document.location");

Website not loading in WebView (javascript issue maybe)

:)
So i'm trying to design an app, and i'm facing a problem i can't find the solution of..
Basically, i designed a Browser app, to load one particular website --> http://www.medishala.org/moonstone13/
But instead of loading the whole page, its giving me only the left corner logo and nothing else..
Other websites are loading fine, facing this problem in only this one. Can anyone please help me out??
The issue might be that the javascript is not compatible with android... Can anybody suggest a way around it??
Thanks. :)
i think JavaScript is disabled in a WebView by Default.
Try to enable it:
//set JavaScript enabled
mywebview.getSettings().setJavaScriptEnabled(true);
This code I used and got the complete website. Here is the code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().requestFeature(Window.FEATURE_PROGRESS);
setContentView(R.layout.placement_layout);
showResults();
}
#SuppressLint("SetJavaScriptEnabled")
private void showResults() {
getWindow().setFeatureInt(Window.FEATURE_PROGRESS,
Window.PROGRESS_VISIBILITY_ON);
mWebView = (WebView) findViewById(R.id.MyWebview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setBuiltInZoomControls(false);
String url = "http://www.medishala.org/moonstone13/";
Log.d("Link", url);
mWebView.loadUrl(url);
final Activity MyActivity = this;
mWebView.setWebChromeClient(new WebChromeClient() {
public void onProgressChanged(WebView view, int progress) {
MyActivity.setTitle("Loading...");
MyActivity.setProgress(progress * 100);
if (progress == 100)
MyActivity.setTitle(R.string.app_name);
}
});
}
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}

YouTube video doesn't play

final String youtube = "<iframe class='youtube-player' type='text/html' width='200px' height='200px' src='http://www.youtube.com/embed/7vcc68CY9Rw' frameborder='0'>";
webview = (WebView) findViewById(R.id.webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginsEnabled(true);
webview.getSettings().setPluginState(PluginState.ON);
webview.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
dialog.show();
return true;
}
public void onPageFinished(WebView view, String url) {
if(dialog.isShowing()) {
dialog.dismiss();
}
}
});
webview.loadData(youtube,"text/html", "utf-8");
I don't know why youtube video doesn't play here, maybe I missed write something? The webview load the data but when I press play button nothing happened there. what is the problem here?
Why not just launch an intent to play the video and then let the user decide if they want their preferred browser or the YouTube app to play the video? When the user presses back they will return to your app. (note this may not work on emulators)
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/watch?v=cxLG2wtE7TM")));
Just download source from here and check it.It works
http://code.google.com/p/html5webview/source/checkout
put this in your webView client
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
// YouTube video link
if (url.startsWith("vnd.youtube:"))
{
int n = url.indexOf("?");
if (n > 0)
{
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse(String.format("http://www.youtube.com/v/%s", url.substring("vnd.youtube:".length(),n)));
}
return (true);
}
return (false);
}
it is better to start an intent with action view to view you tube videos..

Problem in using WebView?

I have created a page which has link to a page of a website. So for showing that on I have used a WebView and it works fine.
My Problem is that when I click on any link given on that webpage, the link opens in phone's default browser view. But I want all the links to be opened in my created WebView.
Have I made any mistake or it is right..
Please Help Me
My code is as follows...
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.e("------------", ".........................................................................................");
setContentView(R.layout.terms_of_use_web_view_page);
btn_back = (Button) findViewById(R.id.terms_of_use_button_back);
btn_back.setOnClickListener(this);
webview = (WebView)findViewById(R.id.terms_of_use_webview);
webview.getSettings().setJavaScriptEnabled(false);
webview.loadUrl("http://www.oomphlink.com/terms-of-use/");
}
Try specifying your own WebViewClient:
WebView webView = (WebView)findViewById( R.id.terms_of_use_webview );
webView.setWebViewClient( new WebViewClient()
{
#Override
public boolean shouldOverrideUrlLoading( WebView view, String url )
{
view.loadUrl( url );
return true;
}
});
To further understand why this is necessary have a look at the documentation for the shouldOverrideUrlLoading method.
wv.setWebViewClient(new MyWebViewClient());
public class MyWebViewClient extends WebViewClient{
}
link for more info...

Categories

Resources