I need to play a youtube video in webview, the link to the video is present in my tag. But when I run my application, only a pic(thumbnail) comes in the place with play icon on it, and after clicking on it, it shows a black screen with a seekbar on it at 00:00 time, and loading symbol on it, but then nothing happens or is shown. It than remains there all day.
I am using Emulator with 4.4 Android.
My code is a follows:-
WebView content = (WebView)findViewById(R.id.webView1);
String html="<html><body>Some text goes here<br>"
+ "<iframe src=\"//www.youtube.com/embed/eu9kMIeS0wQ?rel=0\" height=\"360\" width=\"640\" allowfullscreen=\"\" frameborder=\"0\"/></iframe></body></html>";
content.getSettings().setPluginState(PluginState.ON_DEMAND);
content.getSettings().setJavaScriptEnabled(true);
content.getSettings().setAllowFileAccess(true);
content.setWebChromeClient(new WebChromeClient());
content.setWebViewClient(new WebViewClient());
content.loadDataWithBaseURL("SomeStringForBaseURL", html, "text/html", "UTF-8", "");
I had also included the
android:hardwareAccelerated="true"> In the manifest.
DO I need to add any API's for Youtube or for any other thing. or what sort of mistake I am commencing? Any help will be appreciated. Thanks In Advance.
Well everything is fine, and the video is running nice and fast. Well, the emulator requires flash player to be installed, whereas this code makes videos playable on android devices. If anyone needs the source code, they can use the one above for running the videos in WebView via tag on android.
Related
I am using webview to render a page having videos, but the videos on the page keep lagging. But the same page works very well in chrome.
I have set android:hardwareAccelerated="true" in manifest file and also have added webView.setWebChromeClient(new WebChromeClient()) to my java file but has not helped much (the rendering is better but still has some lagging). Is there any way to use the chrome features in webview so that lagging of video can be removed.
I am using the following: -
Android 7 (Nougat) in Android TV player.
Thanks in advance !!
I have an Android KitKat application with an embedded WebView. The page contains an html5 video element. The video loads fine, but play() has no effect until I tap the play button on the screen, and then play() and pause() work fine. I have also tried myWebView.setWebChromeClient(new WebChromeClient());, but this makes no difference. Also, this same page works fine in Chrome browsers on Mac and Linux.
Well, I found the problem. Android 4+ will not allow autoplay of video or programmatic start of play without first having the user tap the play button on the screen. This is so the user will implicitly acknowledge the use of bandwidth by manually activating playback. While I understand why this would be reasonable for a phone user with a cell connection, my application is a television with an Android HDMI stick. This is a very unfortunate restriction. See more here: Autostart html5 video using android 4 browser
EDIT: There is a workaround - while the stock browser cannot achieve this, using an embedded WebView gives you a little more control:
WebView myWebView = (WebView) findViewById(R.id.webview);
myWebView.setWebChromeClient(new WebChromeClient());
myWebView.getSettings().setMediaPlaybackRequiresUserGesture(false);
This solved the problem!
I'm using a webView in my Android app to load YouTube iframe player and auto play videos. It works fine on Samsung Galaxy S2 & S3, but when runs on Samsung Galaxy S4, it always results in gray screen when trying to auto play.
On Galaxy S4, it works fine without autoplay, needs user action(click) to start playing
(Nothing happened if adding "autoplay:1" in playerVars).
I tried to call player.playVideo() in onPlayerReady(), it resulted in this gray screen:
The LogCat also shows a weird error message:
E/IMGSRV(17004): :0: GetPTLAFormat: Invalid format
when failed to autoplay. I don't know what this message is about; I've googled it and found nothing.
Here's the Android code of WebView:
WebView wv = (WebView) findViewById(R.id.webview);
WebSettings websettings = wv.getSettings();
websettings.setJavaScriptEnabled(true);
websettings.setDomStorageEnabled(true);
websettings.setDatabaseEnabled(true);
wv.loadUrl(strUrl);
wv.setWebViewClient(new WebViewClient());
wv.setWebChromeClient(new WebChromeClient());
wv.setPadding(0, 0, 0, 0);
wv.getSettings().setLoadWithOverviewMode(true);
wv.getSettings().setUseWideViewPort(true);
wv.setVerticalScrollBarEnabled(false);
wv.setHorizontalScrollBarEnabled(false);
Is this a known issue or if there's any solution to autoplay the video? Thanks!
I think disabling autoplay is becoming a "standard".
In iOS Safari, autoplay in HTML5 tags is disabled. (http://developer.apple.com/library/safari/#documentation/AudioVideo/Conceptual/Using_HTML5_Audio_Video/Device-SpecificConsiderations/Device-SpecificConsiderations.html#//apple_ref/doc/uid/TP40009523-CH5-SW4)
In Chrome, autoplay is also disabled. (https://code.google.com/p/chromium/issues/detail?id=159336)
I believe the same happens with the recent versions of the default Android Web Browser.
By the way, you could try this
EDIT:
As the link is dead, I found it in https://lumi.do/p/04x626i1ngvht/how-to-autoplay-html5-video-on-android:
How to "autoplay" HTML5 video on Android
Written by Mathias Desloges in Labs on 19/05/11
Have you ever try to play with the new HTML5 video tag on an Android device?
We have and here is an stange issue which we faced.
Let's set up the context, we want our video starts playing just after the page load complete.
According to the HTML5 specification, we should use the "autoplay" attribute, but it has no effect in Android. So we tried with a little script tha call the play function after the page has loaded:
function callback () { document.querySelector('video').play(); }
window.addEventListener("load", callback, false);
This reproduces well the behavior of the "autoplay" attribute on a
classic desktop browser, but on an Android browser it fails. we
tried to attach the previously defined "callback()" function to a
click event on a arbitrary node, and the video starts playing well "on
click" (normal behavior). And while continue working on the page html
code, we found the solution!
Don't ask me why, but after adding the "poster" attribute it works!
I believe this is a symptom of the OS manufacturers becoming more restrictive in not letting videos autoplay due to bandwidth concerns. This has been an issue on iOS for some time where any JavaScript call to play() will fail unless the user has performed some action like a click first.
That is likely what is going on in your case.
As answered here I believe it's not just restricting auto-play but any auto events. For me it was a page transition in jquerymobile which was triggered when Cordova had loaded. I switched it to being triggered by a click even and this solved the problem.
I have a requirement to play YouTube video content from within an Android application on Google TV. Specifically, we'd like to play it from within a Webview inside a Google TV app. This works on Honeycomb devices (3.1), but does not work on GTV.
Here is the video we are testing with, and how we are linking it via a URL that is loaded into a Webview:
<iframe class="youtube-player" type="text/html" width="400" height="285" src="http://www.youtube.com/embed/u1zgFlCw8Aw" frameborder="0">
The page we are loading in the Webview also WORKS fine when using the regular Browser app (the video plays there, embedded via an iframe, just fine!).
Here are the Webview settings that we are using:
web.getSettings().setJavaScriptEnabled(true);
web.getSettings().setPluginState(PluginState.ON);
When loading the same URL that works in the Browser app from our own app, via a Webview, it fails. The screen shows the initial YouTube video in the box with the controls, but when play is pressed the box turns to plain black and no video plays. SOMETIMES audio plays, but never video.
Logcat shows:
E/libEGL(14956): call to OpenGL ES API with no current context (logged once per thread)
D/ShaderProgram(14956): couldn't load the vertex shader!
I did find an open bug report with another user having the same problem (http://code.google.com/p/android/issues/detail?id=22018) , but there is no follow up there, so I thought I would see if SO gets more attention. Does anyone have any more insight about this issue?
thanks
I've been working on a solution to the same problem. It is working on both tablets and Google TV (including the revue).
The major difference from your code is that I create the HTML code in the android app in code and pass this to webView.loadDataWithBaseURL
webView.loadDataWithBaseURL("http://www.youtube.com", html, "text/html", "utf-8", null);
For more details, please see the source I am working on:
http://code.google.com/p/android-reddittv/source/browse/trunk/RedditTV%20HD%20Android/src/com/elsewhat/reddittv/hd/YoutubePlayerActivty.java
I am new to android. I am trying play a youtube video whose path is present in an html file.. But when I do that I get black screen and upon tapping that a link will be displayed.. But I want the video to be displayed in my webview and on touching that video should be played..
I am using the following code..
webView = (WebView)findViewById(R.id.webView1);
WebSettings websets = webView.getSettings();
websets.setJavaScriptEnabled(true);
webView.loadUrl("file:///android_asset/youtubes.html");
any help on this ?
Thanks,
Keerthi
YouTube usually requires flash. Are your links pointing to desktop version of YouTube? Do this links play in your browser in Android (not opening the YouTube app)?
Also take a look at m.youtube.com - this links are created to run on mobile devices by opening external player.
If you are running it on an emulator, then videos will not play from the youtube as you require flash. But if you are running it on a phone, then check if you added the methods properly or not. Also try using the VideoView class which takes the video source from your SD card. If it works, then it might be some problem with your code or if it doesnt works try installing any external player