Youtube link in html file - android

In webView, I am loading a html file. The file contains text, images and a youtube link. Text and the images are displayed just by loadUrl() method, but video is not being played. How should I get the link and play video.
Format in which link is present in html file is :
<p style="text-align: center; margin: 0px;">
<iframe class="youtube-player" type="text/html" width="300" height="169" src="http://www.youtube.com/embed/4H0fTwtPLfo" allowfullscreen frameborder="0">
</iframe>
</p>
Please guide me.
EDIT
loading html file:
webView.loadUrl("file:///android_asset/"+htmlFile);
webView.setWebViewClient(new VideoWebViewClient());
VideoWebViewClient class:
private class VideoWebViewClient extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView webView, String url) {
webView.loadUrl(url);
return true;
}
}
EDIT 2
I am able to play video by adding following lines of code:
WebSettings webSetting = webView.getSettings();
webSetting.setJavaScriptEnabled(true);
webSetting.setPluginState(WebSettings.PluginState.ON_DEMAND);
webSetting.setAllowContentAccess(true);
webView.loadUrl("file:///android_asset/"+htmlFile);
webView.setWebViewClient(new VideoWebViewClient());
I want to play video in full screen, but the full screen button freeze the video. Can anyone help?

Related

Problem when load html5 videos into webview

I have a problem when load html5 into webview.
My app in first build play video success but when i clear/kill app open again, webview not play video when click play.
Html5
<video src="url.mp4" controls poster="thumbnail.jpg" style="max-width: 100%;">Download video</video>
Source load webview
WebView webView = new WebView(context);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.setWebChromeClient(new WebChromeClient());
webView.setWebViewClient(new WebViewClient());
String html = "<html><head><link rel='stylesheet' href='file:///android_asset/css/style.css'></head><body>"
+ htmlElement.getContent()
+ "</body></html>";
webView.loadDataWithBaseURL("file:///android_asset/", html, mimetype, encoding, "");
I research and try fix this issue but not success
https://stackoverflow.com/questions/3815090/webview-and-html5-video
https://issuetracker.google.com/issues/36935939 v.v..
Anyone has the same my problem? Thanks.
Try this :-
DashBoardFragment2.java :-
https://drive.google.com/file/d/1UREa7uGrQYcF-ABzPktBSjUcOPtytxSC/view?usp=sharing
dashboard.xml :-
https://drive.google.com/file/d/1LlgbdSe-9bgUU5lsGZUGOX3Tg7bnVpzH/view?usp=sharing

Showing iframes in android webview correctly

I want to show some html content in webView. Here is my code:
String htmlContent = "..."; // some content here
webViewContent.getSettings().setJavaScriptEnabled(true);
webViewContent.setWebViewClient(new WebViewClient());
webViewContent.setHorizontalScrollBarEnabled(false);
webViewContent.setHorizontalScrollBarEnabled(false);
webViewContent.getSettings().setSupportMultipleWindows(false);
webViewContent.getSettings().setAllowFileAccess(true);
webViewContent.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.SINGLE_COLUMN);
webViewContent.setWebChromeClient(new WebChromeClient());
webViewContent.setBackgroundColor(ContextCompat.getColor(context, R.color.background_final_page));
webViewContent.loadDataWithBaseURL(null, htmlContent , "text/html", "UTF-8", null);
It shows text and images well, but when i have iframe in htmlContent , for example
<iframe allowtransparency="true" frameborder="0" scrolling="no" src="https://www.facebook.com/plugins/post.php?href=link" width="100%"></iframe>
webView shows iframe not correctly, with cropped height.When i added height="100%" to iframe code, it has no result. When i set height=600 or height=400, it works, but i cannt know in advance height size of iframe.
So, how i can to fix it?

Webview HTML Video fullscreen - onShowCustomView() called only once

I am using HTML video tag to play video in android Webview. However, when I try to press the fullscreen button, the onShowCustomView() from webview chrome client is called only the first time. Every subsequent time I press the fullscreen button, the function onShowCustomView() is not called.
What I want is, every time I press the fullscreen button, onShowCustomView() should be called.
Webview
webView.getSettings().setMediaPlaybackRequiresUserGesture(false);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setAppCacheEnabled(true);
webView.setWebChromeClient(mClient);
webView.setWebViewClient(new WebViewClient());
HTML Video Tag
<div>
<!--<img id="videoImage" src="img/UI_homepage_innovation.png" style="cursor:pointer" onclick="play()"/>
<iframe id="video" style="display:none;" src="http://www.youtube.com/embed/SNMjzPRQUA8?modestbranding=1&rel=0&showinfo=0&autohide=1&fs=0&autoplay=1" frameborder="0" #allowfullscreen></iframe>-->
<video poster="img/UI_homepage_innovation.png" controls>
<source src="video/sintel.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</div>
Webview Chrome Client
public class MyChromeClient extends WebChromeClient {
#Override
public void onShowCustomView(View view, CustomViewCallback callback) {
Log.v("Testing", "onShowCustomView");
}
#Override
public void onHideCustomView() {
Log.v("Testing", "onHideCustomView");
}
}
In order for this to work correctly, I learned that I needed to get the video view Holder and put it in another view. However, I wanted to use my own custom fullscreen view.
In order to do that I made my own custom HTML video controls for using CSS and Javascript and used a 'fullscreen' button to call my custom fullscreen view everytime. Worked like a charm!

Youtube embed videos not working in webview. What's wrong with this code?

I am new to android. I am facing two problems right now 1. I parsed xml file and showed it in webview which contains videos and text . When i click on videos it will not play . In xml videos are youtube embed.
Why is it so?
1.First problem :code and image
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.web_view);
Intent intent=getIntent();
String urlsting=intent.getStringExtra("str");
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.getSettings().setPluginsEnabled(true);
mWebView.setInitialScale(100);
mWebView.getSettings().setBuiltInZoomControls(true);
mWebView.loadDataWithBaseURL(null, urlsting,"text/html", "utf-8", null);
}
When i clcik on video it starts browsing and shows only black screen,then nothing will happen.
![][1]
2. I have you-tube embedded videos url which I given in html tag and try to load it .They are also not working for me. I checked number of questions and blogs and also made no of settings then also unable to show video . Help will be appreciated .Thanks in advance.
String video= "<table bgcolor=\"#666666\"><tr><td><iframe width=\"300\" height=\"260\" frameborder=\"0\" id=\"player\" type=\"text/html\"src=\"http://www.youtube.com/embed/iiLepwjBhZE?enablejsapi=1&origin=example.com\"></iframe></td></tr><tr><td><iframe width=\"300\" height=\"260\" frameborder=\"0\" id=\"player\" type=\"text/html\"src=\"http://www.youtube.com/embed/lBMMTeuJ_UQ?enablejsapi=1&origin=example.com\"></iframe></td></tr><tr><td><iframe width=\"300\" height=\"260\" frameborder=\"0\" id=\"player\" type=\"text/html\"src=\"http://www.youtube.com/embed/BZMkY3y7nM0?enablejsapi=1&origin=example.com\"></iframe></td></tr><tr><td></table>";
mWebView = (WebView) findViewById(R.id.webview);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setPluginState(PluginState.ON);
mWebView.getSettings().setPluginsEnabled(true);
//mWebView.loadDataWithBaseURL(null,load,"text/html","UTF-8",null);
mWebView.loadData(video,"text/html","UTF-8");
In this case I load the youtube embeded videos to webview but they are also not working.
![][2]
I am using android 2.3.3version.I also want clarification that is there any requirement of install adobe flash player on emulator, but i think no because videos are working in browser. can anybody tell i am right or wrong?? please try to give me solution because i stuck with this problem long ago..
I also tried using object tag as follows:
String obj="<object width=\"300\" height=\"260\"><param name=\"movie\" value=\"http://www.youtube.com/embed/iiLepwjBhZE?enablejsapi=1&origin=example.com\"?version=3&hl=pt_BR&rel=0\"></param><param name=\"allowFullScreen\" value=\"true\"></param><param name=\"allowscriptaccess\" value=\"always\"></param><embed src=\"http://www.youtube.com/embed/iiLepwjBhZE?enablejsapi=1&origin=example.com\" ?version=3&hl=pt_BR&rel=0\" type=\"application/x-shockwave-flash\" width=\"480\" height=\"330\" allowscriptaccess=\"always\" allowfullscreen=\"true\" /></object>";
You need to
set a WebChromeClient to your WebView
web.setWebChromeClient(new WebChromeClient());
and turn on hardwareAccelerated value
<application
android:hardwareAccelerated="true"...
check out http://developer.android.com/reference/android/webkit/WebView.html and read HTML5 Video support part.
This works for me...
I guess, you are using iframe tag in your html code. Change it with object tag. I mean, you must use object instead iframe. An example here:
<object width="480" height="330"><param name="movie"
value="http://www.youtube.com/v/"here is your video id"?version=3&hl=pt_BR&rel=0">
</param><param name="allowFullScreen" value="true"></param>
<param name="allowscriptaccess" value="always"></param>
<embed src="http://www.youtube.com/v/"here is your video id"?version=3&hl=pt_BR&rel=0"
type="application/x-shockwave-flash" width="480" height="330" allowscriptaccess="always" allowfullscreen="true" /></object>
Use the code below:
webview.getSettings().setPluginsEnabled(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setAllowFileAccess(true);
webview.loadUrl(url);
Try this and thick it as accepted if your problem solved :)
have you tested your app in actual device? Because the videos which are embedded like this i.e. iFrame are taking more time to get loaded. In my case, same thing was happening then I changed the webview settings to,
getSettings().setPluginsEnabled(true);
but after that it was not working on emulator and in device it was loading so I thought its not working but just today I tried the same thing and it was working fine with my surprise. So you just need to add this line of code.
Second thing is that only those videos which are embedded in iframe tag are supported by enabling plugins to true.
This does it:
myWebView.getSettings().setPluginState(PluginState.ON);
myWebView.getSettings().setJavaScriptEnabled(true);
myWebView.getSettings().setAllowFileAccess(true);
myWebView.setWebChromeClient(new WebChromeClient());
And enable hardware acceleration too!
Follow this source, Youtube video should play
String youtubeId = "iiLepwjBhZE";
int width = rl.getWidth();
int height = rl.getHeight();
WebChromeClient mWebChromeClient = new WebChromeClient(){
public void onProgressChanged(WebView view, int newProgress) {
/*
view.loadUrl("javascript:(function() { " +
"document.querySelector('iframe').setAttribute('style', 'width: 1080px; height: 1920px'); " +
"})()");
*/
}
};
String video= "<table bgcolor=\"#666666\"><tr><td><iframe width=\"" +
width +
"\" height=\"" +
height +
"\"frameborder=\"0\" id=\"player\" type=\"text/html\"src=\"http://www.youtube.com/embed/" +
youtubeId +
"?enablejsapi=1&origin=example.com\"></iframe> </td> </tr><tr><td></table>";
//
webView.getSettings().setPluginState(PluginState.ON);
webView.setWebChromeClient(mWebChromeClient);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setAppCacheEnabled(true);
webView.setInitialScale(1);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setUseWideViewPort(true);
//mWebView.loadDataWithBaseURL(null,load,"text/html","UTF-8",null);
webView.loadData(video,"text/html","UTF-8");

How to play media files (3GP/swf files) locally inside webview

Iam looking for playing media files inside webview Can you please help me out ..what iam missing..I loaded webview as : _webview.loadUrl("file:///android_asset/3gp.html"); and the code inside my html file is as:
<body>
<object width="550" height="400">
<param name="movie" value="sample.3gp">
<embed src="file:///android_asset/sample.3gp" width="550" height="400">
</embed>
</object>
</body>.
Iam not getting the output. Please hellp me out ..Thanks in advance
To play SWF files you have to enable plugins:
_webview.getSettings().setPluginState(PluginState.ON);
and if your .html file uses javascript:
_webview.getSettings().setJavaScriptEnabled(true);
EDIT to answer comment
In your situation you need to use your own webview at the moment links are redirecting to the browser:
_webview.setWebViewClient(new DownloadWebViewClient());
public class DownloadWebViewClient extends WebViewClient{
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false; // Allows your webview to handle clicked links
}
}

Categories

Resources