From last one week i try to find how to play a html5 video in android webview i read so many blog and questions in the stack overflow but my problem is solve.i am using .mp4 , .wevm ,ogv and also .3gp format but noon of format is running.
i run these videos in the two different device one is micro-max funbook with android 4.0 and the another device is a Samsung device with 2.3.3 android os.
this is my code.
url="file://"+Environment.getExternalStorageDirectory()+"/unzipped/HTML5/res/test1.html";
webview = (WebView) findViewById(R.id.webview);
webview.setWebChromeClient(new WebChromeClient());
webview.getSettings().setPluginsEnabled(true);
webview.getSettings().setPluginState(PluginState.ON);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
webview.loadUrl(url);
For API level 11+ it should suffice that you add the attribute android:hardwareAccelerated="true" to the <application> tag in your Android manifest file.
For older APIs you need to implement the methods onShowCustomView and onHideCustomView in your your own WebChromeClient.
Or you can check out the html5webview
Related
I'm having some trouble trying to get this video playing in my webview. The youtube video appears but whenever I attempt to play the video, all it does is continuously load. How do I fix this? Also, I have the hardware acceleration on true. Here's my current code for playing the video:
String frameVideo = "<html><body>Video From YouTube<br><iframe width=\"420\" height=\"315\" src=\"https://www.youtube.com/embed/_4IRMYuE1hI\" frameborder=\"0\" allowfullscreen></iframe></body></html>";
WebView displayYoutubeVideo = (WebView) findViewById(R.id.webView);
displayYoutubeVideo.setWebChromeClient(new WebChromeClient());
displayYoutubeVideo.setWebViewClient(new WebViewClient() {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
WebSettings webSettings = displayYoutubeVideo.getSettings();
webSettings.setJavaScriptEnabled(true);
displayYoutubeVideo.loadData(frameVideo, "text/html", "utf-8");
The youtube video appears but whenever I attempt to play the video,
all it does is continuously load. How do I fix this?
I had the same issue, on a real device (emulator won't work AFAIK). In order to fix that, you need to enable the hardware acceleration for your app. You can enable it by adding:
android:hardwareAccelerated="true"
to your AndroidManifest.xml file. E.g:
<application android:hardwareAccelerated="true" ...>
I have an application in which I am using Mozilla PDF for viewing PDFs.
Mozilla PDF uses web view to load PDFs.
The PDF files are large, and are all images actually.
The application works fine, but the PDF loads extremely slow on all Samsung devices. The application works fine on other devices, but is so slow on Samsung that it's really useless.
I have the following settings for the web view:
settings.setJavaScriptEnabled(true);
settings.setRenderPriority(RenderPriority.HIGH);
settings.setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
I also have set a WebChromeClient.
Does anyone knows why am I facing this issue? And only on Samsung devices?
Any workarounds? Any help would be greatly appreciated.
Here’s a WebView setup to display PDFs using PDF.js. It works fine on all devices (that are running at least API 19), including old tiny about-to-die Samsungs.
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
webView.getSettings().setSupportMultipleWindows(true);
webView.getSettings().setLoadsImagesAutomatically(true);
webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH);
webView.getSettings().setPluginState(WebSettings.PluginState.ON);
webView.getSettings().setPluginState(WebSettings.PluginState.ON_DEMAND);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN)
{
webView.getSettings().setAllowFileAccessFromFileURLs(true);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
{
webView.getSettings().setMediaPlaybackRequiresUserGesture(true);
}
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT)
{
webView.getSettings().setAllowUniversalAccessFromFileURLs(true);
}
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setDomStorageEnabled(true);
webView.setWebViewClient(new WebViewClient());
webView.setWebChromeClient(new WebChromeClient());
// should enable pinch to zoom....
webView.getSettings().setSupportZoom(true);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setDisplayZoomControls(false);
String webViewUrl;
try
{
/*
Try encoding the PDF file URL. I think i’ve read that this helps with
load/rendering speed?
*/
webViewUrl = "file:///android_asset/pdfjs/web/viewer.html?file=" + URLEncoder
.encode(pdfUri.toString(), "UTF-8");
}
catch (UnsupportedEncodingException e)
{
// It works without encoding it (at least in my case) so just display it with the string as is.
Log.e("Error encoding PDF file URL, will display without encoding.", e);
webViewUrl = "file:///android_asset/pdfjs/web/viewer.html?file=" + pdfUri.toString();
}
webView.loadUrl(webViewUrl);
Not sure if this will help, but thought I’d post a full WebView setup to support PDF.js, which I’ve never run into speed issues with. Good luck.
Note: Might also be a good idea to turn hardware acceleration on in your application’s manifest. This is only available on API 14 and above. By now this shouldn’t be an issue (if it is, my heart goes out to you).
<application
android:name="com.pdfjs.webview.app.McAwesome"
//...
android:hardwareAccelerated="true">
</application>
Following code is used to play embedded youtube videos, on a sample app.
The embedded videos play fine in android version 19, 20 etc., but DO NOT play on device of version 10.
My question is two-fold:
a. How do we find the solution (except asking someone of-course ) ?
Javadocs do not have info. about this.
b. How do we solve this problem ?
webView = (WebView) findViewById(R.id.webView);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebChromeClient(new WebChromeClient());
webView.getSettings().setAllowFileAccess(true);
//webView.getSettings().setAllowContentAccess(true);
webView.getSettings().setPluginState(PluginState.ON);
webView.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
//return super.shouldOverrideUrlLoading(view, url);
view.loadUrl(url);
return true;
}
});
webView.loadUrl("http://youtu.be/7uq-s0dMLsE");
Thanks for all the help in advance !!
I'm unable to open you tube into my application's webview on android 2.3 but on android 4.1 it works properly
here is the code
openWebLinkWebView.getSettings().setJavaScriptEnabled(true);
openWebLinkWebView.getSettings().setBuiltInZoomControls(true);
openWebLinkWebView.getSettings().setRenderPriority(RenderPriority.HIGH);
openWebLinkWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
openWebLinkWebView.getSettings().setAppCacheEnabled(false);
openWebLinkWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
openWebLinkWebView.getSettings().setLightTouchEnabled(false);
openWebLinkWebView.getSettings().setUseWideViewPort(true);
openWebLinkWebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
openWebLinkWebView.getSettings().setPluginsEnabled (true);
openWebLinkWebView.getSettings().setSupportMultipleWindows(true);
openWebLinkWebView.getSettings().setPluginState(PluginState.ON);
openWebLinkWebView.loadUrl(url);
you can use this code in your YouTube video Activity class
WebView web=new WebView(MyYouTubeActivity.this);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("youtube_link");
setContentView(mWebView);
Yes i faced this issue before this is because the feature of android:hardwareAccelerated="true" is only available in API version greater or equal to 11 and in 4.1 it is by default enabled (you can enable/disable this in manifest at application level).
I had a problem like yours when playing on 2.3.3
The following worked for me.
webView = (WebView) findViewById(R.id.idWebView);
webView.setWebChromeClient(new WebChromeClient(){
});
webView.setWebViewClient(new WebViewClient());
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setPluginsEnabled(true);
webView.getTouchables();
webView.setBackgroundColor(Color.parseColor("#00000000"));
String emdLink = "http://www.youtube.com/embed/" + video_code;
webView.loadUrl(emdLink);
Note: you should have embed + your video code.
Note also the above uses a deprecated call - in place of
webView.getSettings().setPluginsEnabled(true);
it should be better to use (untested):
webView.getSettings().setPluginState(PluginState.ON);
I want to put a web into webview.These is a video in the web.The video's format is wmv.I put the web in the android project "assets" file.Now I can scan the web,but the video can't play.This is the code on below.Who can help me?Thanks.
public class WebViewActivity extends Activity {
private WebView webview;
private WebSettings webSettings;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webView);
webSettings = webview.getSettings();
webSettings.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);
webSettings.setBuiltInZoomControls(true);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setPluginState(PluginState.ON);
webview.getSettings().setPluginsEnabled(true);
webview.loadUrl("file:///android_asset/demo/html/demo.html");
webview.setWebViewClient(new MyWebClient());
}
private class MyWebClient extends WebViewClient {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
WMV is a proprietary Microsoft video format. Chances are Android doesn't have a codec for it. You should convert it to something standard e.g H264.
Unfortunately video encoding is a minefield of proprietary tech. There are a few open source converters available. FFMPEG is probably the most extensive. There's a handy GUI for this called WinFF (if you're using windows).
You can test your HTML file by putting it and your video onto the SDcard and see if it works in it's simplest form. Try with different video formats as a sanity check.
WMV isn't widely supported on Android devices, but there are various tools to convert format.
https://gist.github.com/3718414 is a sample showing an Android webview and HTML5 player that works with the video codecs that Android supports (.mp4 is best for progressive video as it has by far the widest support)