play youtube video in WebView

后端 未结 7 2187
抹茶落季
抹茶落季 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 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...

提交回复
热议问题