Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Youtube HTML5 Video Stopped Working in Android

Somehow Youtube HTML5 video stopped working for me about a week ago. I have no idea why. Here's the code that was working last week (well, not the real code, but the smallest example I could make):

public class VideoTestActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        WebView webView = new WebView(this);
        webView.getSettings().setJavaScriptEnabled(true);
        webView.getSettings().setPluginState(PluginState.OFF);
        webView.setWebChromeClient(new TestWebChromeClient());

        setContentView(webView);

        // Try with http://player.vimeo.com/video/24158845 and it works.
        webView.loadUrl("http://www.youtube.com/embed/e2UIg3Ddfp0");
    }

    private class TestWebChromeClient extends WebChromeClient {
        @Override
        public void onShowCustomView(View view, WebChromeClient.CustomViewCallback callback) {
            super.onShowCustomView(view, callback);
            VideoTestActivity.this.setContentView(view);
        }
    }
}

This doesn't work on my phone or the emulator. The crazy part is that Vimeo still works great. I tried setting an iPhone user agent, but that didn't work and I'm out of ideas. It really looks like Youtube has changed something...

like image 578
Phil Kulak Avatar asked Sep 15 '11 23:09

Phil Kulak


1 Answers

Youtube may have changed something in their embedded page and most likely this is causing problems inside the WebView and that is why it is not working anymore.

Also are you testing your app on the same device as before?

I don't think it is a good practice to use this solution, since you can not rely that the content that you display today from Youtube and it works, will also be the same in a few days and it will still work, as you have just encountered now. I had also similar problems with videos and Youtube content, it would work on one device, and not at all on some other devices. It is also a Flash content, and a lot of Android devices don't have Flash and will not be able to play the content.

A solution would be to create an Intent for playing the video:

      Intent videoIntent=new Intent(Intent.ACTION_VIEW, 
                                     Uri.parse("http://www.youtube.com/my_url"));
      startActivity(videoIntent);

But what I would recommend is to encode the videos for Android as suggested in the Supported Media Formats and store them on your own server if you have the possibility.

EDIT: They definetly changed something since there is an js error there. You can track them by using the WebChromeClient:

  mWebView.setWebChromeClient(new WebChromeClient(){
        @Override
        public void onConsoleMessage(String message, int lineNumber,
                String sourceID) {
            super.onConsoleMessage(message, lineNumber, sourceID);
            Log.i("VIDEO VIEW",message);
        }
 }

This is the message error I got:

TypeError: Result of expression 'a' [undefined] is not an object.line 78sourcehttp://www.youtube.com/embed/e2UIg3Ddfp0

I don't know if it helps you, I'll have another look later and see what I can find.

like image 105
Ovidiu Latcu Avatar answered Oct 25 '22 09:10

Ovidiu Latcu