26

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?

Community
  • 1
  • 1
Yeo
  • 11,416
  • 6
  • 63
  • 90

2 Answers2

7

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

N20084753
  • 2,130
  • 2
  • 17
  • 12
5

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");
Blundell
  • 75,855
  • 30
  • 208
  • 233
  • I didn't overriding URL loading, and based on the code you provided I tried but doesn't affect the output. Yes, The video won't play but the website loads normally.. I tried lying to Youtube based on your code, but they ask for Flash Player.. – Yeo Jun 12 '11 at 17:23
  • Is that not why it doesn't load then? No flash on your device? have you tried setPluginState(PluginState.On_DEMAND); , perhaps it will ask for flash and explain your device doesn't have – Blundell Jun 12 '11 at 17:51
  • Yes, it's true, But I purposely do not want to have a flash on my device yet. Because I want it to play under HTML5. Reason because a lot of customer using Android without flash capability. So I am trying to develop the youtube as HTML5 instead of flash. – Yeo Jun 12 '11 at 18:04
  • 1
    but that video isn't loaded with html5, how can you prove it is? On this website: http://www.youtube.com/html5 it says you have to sign in then opt in for HTML5 video playback. Or do you know something else? – Blundell Jun 12 '11 at 19:03
  • um... you're right,. To use HTML5 feature, we must opt in... But how come on my Android Browser can play youtube HTML5 video smoothly? – Yeo Jun 13 '11 at 01:21
  • 1
    @YeoEoeY Maybe it's doing a redirect to a non html5 site? Least we solved your first question :-) – Blundell Jun 13 '11 at 08:21