Play Youtube HTML5 embedded Video in Android WebView - android

I am having a problem running a HTML5 Youtube embedded in a WebView.
I want to play a Youtube video on my application. I decided to use WebView instead of VideoView, because I want to make my system more flexible to play video from web.
Although There are many ways to get play youtube on the android, but I will use the youtube embedded version. "http://www.youtube.com/embed/___________________". Because this is one of the solution I found when your android doesn't support flash.
The problem:
The WebView load as normal including the embedded Youtube. But I get a black screen on the youtube at start.
When I click on it. It load the first Image only but then It is not Playing. I tried to play on the android browser, it works smoothly but not in the webView.
Any idea why?
Below is just a snip of my code:
WebView wv = new WebView(getApplicationContext());
wv.getSettings().setPluginState(PluginState.ON);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl("http://apiblog.youtube.com/2010/07/new-way-to-embed-youtube-videos.html");
setContentView(wv);
Here are some of the resources that I found very useful:
How can we play YouTube embeded code in an Android application using webview?
play youtube video in WebView
How to embed a YouTube clip in a WebView on Android
http://apiblog.youtube.com/2010/07/new-way-to-embed-youtube-videos.html
http://www.broken-links.com/2009/10/06/building-html5-video-controls-with-javascript/
http://www.youtube.com/embed/bHQqvYy5KYo
Thank you in advance for any support and help :)
Update (13 June 2011):
I successfully load the http://m.youtube.com inside the WebView, but unable to play any video. But When I tried to load the URL on my Android Browser, it can play.
From here, I notice that the youtube site from my WebView is not signed in. So How can we allow the WebView to use the same credential as my Youtube account in my phone? Will it actually works?

Step 1 : Simply add this to Manifest file
android:hardwareAccelerated="true"
step 2 : check if you are setting layer for your webview.
(i.e.)
//myWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);
setting hardware acceleration to true and commenting these lines worked for me .
To know more about Hardware Acceleration and Layers look here at http://developer.android.com/guide/topics/graphics/hardware-accel.html

EDIT
So from the comment conversation we have deducted:
On this website: www.youtube.com/html5 it says you have to sign in then opt in for HTML5 video playback
That is why your video will not load, it is redirecting to a flash version and your webview does not have flash.
ORIGINAL
Are you overriding url loading so your webview is being used?
wv.setWebViewClient(new WebViewClient(){
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
Are you saying the video won't play but the website loads?
You could try lying to YouTube and telling them your a different browser (perhaps pretend to be the android browser) Firefox is:
wv.getSettings().setUserAgentString("Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/534.36 (KHTML, like Gecko) Chrome/13.0.766.0 Safari/534.36");

Related

Is Android Webview is same as the Chrome browser?

I am having the webview, which will be created using the following code.
WebView mWebView=new WebView(this);
mWebView.setWebViewClient(new WebViewClient());
mWebView.setWebChromeClient(new WebChromeClient());
mWebView.addJavascriptInterface(new AndroidJavaInterface(), "JavaInterface");
Is this webview is same as the chrome.? If I am loading any URL into this webview using loadurl() method, and running in two different devices, Is the webview is same in all the devices?. I think it will run on the customized mWebView or Is the app pick the device's default browser for loading URL?
EDIT 1: I have created the webview in android version 2.2. That is API 8. If I am running the app in android 4.1 device. Is the webview version is changed or the webview is remain same for all the devices with different versions?
No, Webview widget is not Chrome.
You can implement a WebView adding some behaviour like Javascript and Java, but IT WILL NEVER BE LIKE A BROWSER (less performance,less capability...).
Well according to my knowledge WebView and Chrome browser are different.
WebView is an Android widget which is used to display local and remote web pages. Even though it has got some features of a browser like rendering JavaScript, it cannot be considered as a fully fledged browser.
Please refer Developer Site for more details.

How to play inline html5 video in Android Browser

I'm doing mobile web app, in that i use html5 to play videos, it works fine
But In Android native browser if i click on the video it transfer to native media player. but i want to play it inline
How to do this? thanks
Piperoman wrote his answer in comments, so I figured I would put the link into an answer for convenience:
HTML5 <video> element on Android

How to load youtube website in Android WebView?

I have a very simple WebView implementation, it works well in almost all websites, but when it comes to youtube, I can load the home page, video page, but when I click play button (on page m.youtube.com/index?desktop_uri=%2F&gl=US#/watch?v=xxxxxx), the video flash but does not play. My android version is 2.3.
first update your flash player ....
second check for permission like you have given for internet or not in androidMenifest file
third
i dont know why but webviewclient class causes the problem , we can run it in default browser remove the webviewclient class and simly load the url of youtube you will be able to run it on 2.2,2.3
w=(WebView)findViewById(R.id.webview);
w.getSettings().setJavaScriptEnabled(true);
w.getSettings().setPluginsEnabled(true);
w.loadUrl("http://www.youtube.com");
for 2.1 just add this, use intent and through parse method open it through default installed youtube application
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com/v/%s")))); }

Launch youtube app from Android browser

It used to be that if you clicked on an appropriate embed tag on a web page in the Android browser, it would launch the youtube app. Now that the browser has its own youtube plugin, it seems to run that instead.
How can I get the old behavior?
Youtube's web pages still have the old behavior, but I can't understand their javascript.
Thanks,
Dawg
A link like
YouTube Video
with video id does the trick. See, for example,
http://it-ride.blogspot.com/2010/04/android-youtube-intent.html

play youtube video in WebView

In my android app I have a WebView to display html data from our website. Sometimes the page will have youtube embed objects. This doesn't show up properly in the app. Is there any way to show/play youtube videos in WebView ? Thanks.
You cannot show them embedded except perhaps on devices that have Flash.
However, if you can parse out the YouTube video details, you may be able to construct an ACTION_VIEW Intent that will show them on the YouTube application...for those Android devices that have the YouTube application.
You might also experiment with HTML5's <video> tag, which AFAIK is supported in the Browser application and may therefore work in WebView.
I came accross this post: link
And indeed, I basically only needed to add to the application manifest xml:
android:hardwareAccelerated="true"
And voila, even the youtube video's started playing
webView.setWebViewClient(new WebViewClient()
{
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);
}
});
You WebView (webView) will send you the shouldOverrideUrlLoading message with a URL that looks like this:
vnd.youtube:{VIDEO_ID}?{PARMS}
Parse this to convert it to http://www.youtube.com/v/{VIDEO_ID}, then hand off this revised URL as an Intent.
Works for me...
Read my post on the android-developers group here: YouTube in the emulator?
Basically, the best way to play YouTube clips is to create your own Activity for it, and here's a great example: Polish Your App: Free Embeddable Android YouTube Activity!
UPDATE: The problems with the incompatibilities due to YouTube token changes have been fixed. Latest version of the component should work just fine for public YouTube videos.
You could try switching your website to embed the HTML5 version of the YouTube player instead of the flash version. Still not sure this will work 100%, but it's obviously going to work better than the flash version on devices that don't currently support flash.
Edit: Nevermind it looks like the HTML5 version also requires the browser to support the H.264 codec, which it doesn't look like any Android devices currently support.
Embedded youtube videos work fine in desktop browsers and in iPhone browsers (even when embedded in apps on iPhone), so it seems to be problem with Android rather than YouTube.
There is a library I use for html5 video tags in the android WebView, it works for all youtube videos and most flash videos as well, supporting entering and exiting fullscreen among other features.
VideoEnabledWebView by cprcrack

Categories

Resources