play youtube video in WebView
In my android app I have a WebView to display html data from our website. Sometime开发者_开发知识库s 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
精彩评论