开发者

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...


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜