play youtube video in WebView

后端 未结 7 2121
抹茶落季
抹茶落季 2020-11-27 15:44

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 t

相关标签:
7条回答
  • 2020-11-27 15:51

    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

    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-11-27 15:56

    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.

    0 讨论(0)
  • 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

    0 讨论(0)
  • 2020-11-27 16:03
    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...

    0 讨论(0)
  • 2020-11-27 16:13

    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.

    0 讨论(0)
提交回复
热议问题