I want to play a music from any music app(gaana/wynk/saavn etc) using webview android. My requirement is I want to open a music app in webview and user can play music directly using webview just like we open (gaaan/wynk/saavn) in web browser of mobile device and you can play any song.
I tried gaana and wynk music, both are able to open on webview but when i am clicking on any songs that songs does not play. do i need to enable any settings for that i already have enabled javascript.
below is the code i have tried.
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl(url);
webView.setWebViewClient(new WebViewClient()
{
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
}
});
Please suggest any solution for this.
its because app cant use storage.
WebView webView = (WebView) findViewById(R.id.webview);
webview.getSettings().setDomStorageEnabled(true);
This solves the problem !
Related
I have webview displayed on custom dialog, I am loading an URL to WebView which is video URL, by default the video is loaded and starts playing, I want to disable auto play function. Here is my code for WebView
wvActiveVideo.setWebViewClient(new WebViewClient());
wvActiveVideo.getSettings().setJavaScriptEnabled(true);
wvActiveVideo.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wvActiveVideo.getSettings().setPluginState(WebSettings.PluginState.ON);
wvActiveVideo.getSettings().setMediaPlaybackRequiresUserGesture(false);
wvActiveVideo.setWebChromeClient(new WebChromeClient());
wvActiveVideo.setWebViewClient(new WebViewClient());
wvActiveVideo.getSettings().setSupportZoom(true);
wvActiveVideo.getSettings().setBuiltInZoomControls(true);
wvActiveVideo.getSettings().setDisplayZoomControls(false);
String url = model.getVideo();
wvActiveVideo.loadUrl(model.getVideo());
It depends of your algorithm.
First advice , before load url show your user play button background.
Second advice is data manupulation ;
#Override
public void onPageFinished(WebView view, String url) {
view.loadUrl("javascript:document.getElementById(\"video-test\").innerHTML = \"\";void(0);");
webView.setVisibility(View.VISIBLE);
}
I have a webview where I perfectly load a google form. In the google form there is a link that redirect to a web page, something like "https://www.google.com/url?q=http://www.mywebsite.com/support/" but no matter what I do I cannot open this link. Everything else work fine.
WebSettings webSettings = supportWebPageWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
supportWebPageWebView.setWebViewClient(new WebViewClient() {
public void onPageStarted(WebView view, String url, Bitmap favicon) {
//do stuff
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, final String url) {
//do stuff
}
});
The only way to make it work is open the link in a browser.
Anyone had the same problem?
I haven't noticed that clicking on the link it opens a new window, the answer is than here Android - Open target _blank links in WebView with external browser
I am new in android and I am displaying a news link in a webview. News link contains a video. Problem is that, After opening a link i have to click on video then video is playing but i want that video should be play automatically.
Thanks in advance.
My code is:
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
myWebView.getSettings().setPluginState(PluginState.ON);
myWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) { web.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); }
});
myWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.loadUrl("http://aajtak.intoday.in/livetv.html");
myWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
https://developer.android.com/reference/android/webkit/WebSettings.html#setMediaPlaybackRequiresUserGesture(boolean)
Note: Only for API level 17 and above.
This works for me.
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
using above, after opening a link no need to click on video, the video is playing automatically.
video on webview didn't support 'autoplay'.
so we shoud start video by hand:
android:
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
view.loadUrl("javascript:onPageFinished();");
}
JS:
function onPageFinished() {
var video = document.getElementById("video");
video.play();
}
myWebView.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {web.loadUrl("javascript:(function() { document.getElementsByTagName('video')[0].play(); })()"); }
});
You should implement loadUrl on the WebView...
Simply replace web.loadUrl with view.loadUrl and it should work just fine
Just make your webview thinking he is running in PC instead mobile
Insert this os MainActivity file
webView.getSettings().setUserAgentString("1");//for desktop 1 or mobile 0.
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..
I have loaded an external URL in my WebView. Now what I need is that when the user clicks on the links on the page loaded, it has to work like a normal browser and open the link in the same WebView. But it's opening the default browser and loading the page there?
I have enabled JavaScript. But still it's not working. Have I forgotten something?
If you're using a WebView you'll have to intercept the clicks yourself if you don't want the default Android behaviour.
You can monitor events in a WebView using a WebViewClient. The method you want is shouldOverrideUrlLoading(). This allows you to perform your own action when a particular URL is selected.
You set the WebViewClient of your WebView using the setWebViewClient() method.
If you look at the WebView sample in the SDK there's an example which does just what you want. It's as simple as:
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
in some cases you might need an override of onLoadResource if you get a redirect which doesn't trigger the url loading method. in this case i tried the following:
#Override
public void onLoadResource(WebView view, String url)
{
if (url.equals("http://redirectexample.com"))
{
//do your own thing here
}
else
{
super.onLoadResource(view, url);
}
}
Official documentation says, click on a link in a WebView will launch application that handles URLs. You need to override this default behavior
myWebView.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
or if there is no conditional logic in the method simply do this
myWebView.setWebViewClient(new WebViewClient());
Add this 2 lines in your code -
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient());
The method boolean shouldOverrideUrlLoading(WebView view, String url) was deprecated in API 24. If you are supporting new devices you should use boolean shouldOverrideUrlLoading (WebView view, WebResourceRequest request).
You can use both by doing something like this:
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
newsItem.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
});
} else {
newsItem.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
}
Arulx Z's answer was exactly what I was looking for.
I'm writing an app with Navigation Drawer with recyclerview and webviews, for keeping the web browsing inside the app regardless of hyperlinks clicked (thus not launching the external web browser). For that it will suffice to put the following 2 lines of code:
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.setWebViewClient(new WebViewClient());
exactly under your WebView statement.
Here's a example of my implemented WebView code:
public class WebView1 extends AppCompatActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebView wv = (WebView) findViewById(R.id.wv1); //webview statement
wv.setWebViewClient(new WebViewClient()); //the lines of code added
wv.setWebChromeClient(new WebChromeClient()); //same as above
wv.loadUrl("http://www.google.com");
}}
this way, every link clicked in the website will load inside your WebView.
(Using Android Studio 1.2.2 with all SDK's updated)