Auto play video in webview - android

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.

Related

Unable to disable autoplay for video from a URL in WebView

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);
}

wynk OR Gaana music not playing on webview android

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 !

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..

YouTube link on WebView do not open Youtube

I have been working on an appliction, the requirement states that i need to open a Youtube link on the web view. Once clicked on the video i need to open the Youtube application to play the video.
After lots of try had not been quite successful to achieve this goal!
Looking forward for sugestions.
this is what i tried.
wb = (WebView)findViewById(R.id.web);
wb.getSettings().setJavaScriptEnabled(true);
wb.setWebViewClient(new MyClient());
wb.getSettings().setPluginsEnabled(true);
wb.getSettings().setPluginState(PluginState.ON_DEMAND);
wb.getSettings().setAllowFileAccess(true);
wb.getSettings().setAppCacheEnabled(true);
wb.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
wb.loadUrl("http://m.youtube.com");
private class MyClient extends WebViewClient {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
mHandler.post(new Runnable() {
public void run() {
if(!pgd.isShowing()){
pgd.show();
}
}
});
super.onPageStarted(view, url, favicon);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
Log.i("url loaded", "url::: " + url);
view.loadUrl(url);
return true;
}
#Override
public void onPageFinished(WebView view, String url) {
mHandler.post(new Runnable() {
public void run() {
if(pgd !=null && pgd.isShowing()){
pgd.dismiss();
}
}
});
super.onPageFinished(view, url);
}
}
Any pointers will be highly appreciated.
Thanks.
Well if you have the URL of video and also have youtube's application installed on android device, I have better idea.
Use startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("url of youtube video")));
This will pop up user and will display option of browser and YouTube, and if YouTube is selected the video will be played in YouTube player.
I faced the same issue and got the solution by adding this line:
wb.getSettings().setUserAgentString("Mozilla/5.0 (Linux; U; Android 2.0; en-us; Droid Build/ESD20) AppleWebKit/530.17 (KHTML, like Gecko) Version/4.0 Mobile Safari/530.17");
Hope it works for you as well.
Try this in onCreate():
WebView webview = new WebView(this);
String htmlString = "<html> <body> <embed src=\"link of youtube video\"; type=application/x-shockwave-flash width="+DeviceWidth+" height="+DeviceHeight+"> </embed> </body> </html>";
webview.loadDataWithBaseURL(null,htmlString ,"text/html", "UTF-8",null);
webview.setWebViewClient(new MyClient());
You need to implement the WebChromeClient and set it as the client of the webview. Then handle the video playback in the onShowCustomView callback.
If you want more control over where a clicked link loads, create your own WebViewClient that overrides the shouldOverrideUrlLoading() method. MyWebViewClient is an inner class of Activity.
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebViewClient(new MyWebViewClient());
private class MyWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
if ("www.example.com".equals(request.getUrl().getHost())) {
// This is my website, so do not override; let my WebView load the page
return false;
}
// Otherwise, the link is not for a page on my site, so launch another Activity that handles URLs
Intent intent = new Intent(Intent.ACTION_VIEW, request.getUrl());
startActivity(intent);
return true;
}
}

Clicking URLs opens default browser

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)

Categories

Resources