Android – Play Youtube HTML5 embedded Video in Android WebView

androidhtmlvideowebviewyoutube

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:

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?

Best Answer

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