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
Related
I cant use WebView, please help me
I will try my best & I hope I can solve it here.
check out official release of YouTube Android Player API, which plays yt videos natively, without any web-side
and if you want/need web player, but you can't use WebView - you may implement "FirefoxView", web engine used by Mozilla in their Android software, not related to Chromium at all, called GeckoView
You can use lib from opensource: link
I used this code to play youtube video using native youtube app.
startActivity(new Intent(Intent.ACTION_VIEW,Uri.parse("vnd.youtube:zAhzo0JCbFY")).putExtra("force_fullscreen",true));
It works fine. But thing is its a clickable video like this:
http://www.youtube.com/watch?v=zAhzo0JCbFY
Youtube app doesn't allow the links to be clicked. Is there anything to be done(only with native youtube app)?
SORRY
The only thing you can do is to wait for the YouTube app to implement this.
BUT
You can find some alternative, including showing the video in a webview (if you manage), showing the links natively (if you have them), or just using the browser.
In all these cases, it is not the Youtube app.
You can do this by using <intent-filter>. I do not now the exact procedure but this explains it well.
I did google about this question and I found 4 possible ways to do it:
play the video with the following manner:
startActivity(newIntent(Intent.ACTION_VIEW,Uri.parse(uri)));
Get the RTSP link and play the video with VideoView
play the video using Flash
Using HTML5 iframe
I have an android 4.0 device and I am able to play YouTube videos using the first three methods. My question is: which method is the most adaptable to different version. If the answer is using HTML5, how can I do it. Thank you very much for your time.
The most allround way of doing it to open it with a URI parse. There is also a way of forcing the YouTube app to be called(sorry I think I read it someplace but I do not remember how) and show the video there. This means you do not get the option to chose to open the video in the browser.
The rest of the alternatives are only supported by some devices. (Flash is outdated, HTML5 is not supported on most devices). I you are going to do something else then using the buildt-in YouTube app I would recomend RTSP as the best option.
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");
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