Android 在 WebView 中播放 YouTube 视频

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2292086/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:17:37  来源:igfitidea点击:

play youtube video in WebView

androidandroid-widget

提问by Ionic Walrus

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 there any way to show/play youtube videos in WebView ? Thanks.

在我的 android 应用程序中,我有一个 WebView 来显示来自我们网站的 html 数据。有时页面会有 youtube 嵌入对象。这在应用程序中没有正确显示。有没有办法在 WebView 中显示/播放 youtube 视频?谢谢。

采纳答案by CommonsWare

You cannot show them embedded except perhaps on devices that have Flash.

除非在具有 Flash 的设备上,否则您无法显示它们嵌入。

However, if you can parse out the YouTube video details, you may be able to construct an ACTION_VIEWIntentthat will show them on the YouTube application...for those Android devices that have the YouTube application.

但是,如果您可以解析出 YouTube 视频详细信息,您也许能够构建一个ACTION_VIEWIntent将它们显示在 YouTube 应用程序上的...对于那些具有 YouTube 应用程序的 Android 设备。

You might also experiment with HTML5's <video>tag, which AFAIK is supported in the Browser application and may therefore work in WebView.

您还可以尝试使用 HTML5 的<video>标签,浏览器应用程序支持 AFAIK,因此可以在WebView.

回答by Ronnie

I came accross this post: link

我遇到了这篇文章:链接

And indeed, I basically only needed to add to the application manifest xml:

事实上,我基本上只需要添加到应用程序清单 xml 中:

android:hardwareAccelerated="true"

And voila, even the youtube video's started playing

瞧,连 YouTube 视频都开始播放了

回答by CSmith

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:

您的 WebView (webView) 将向您发送 shouldOverrideUrlLoading 消息,其 URL 如下所示:

vnd.youtube:{VIDEO_ID}?{PARMS}

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.

解析它以将其转换为http://www.youtube.com/v/{VIDEO_ID},然后将此修改后的 URL 作为 Intent 传递。

Works for me...

对我有用...

回答by Felix

Read my post on the android-developers group here: YouTube in the emulator?

在这里阅读我在 android-developers 组上的帖子:模拟器中的 YouTube?

Basically, the best way to play YouTube clips is to create your own Activity for it, and here's a great example: Polish Your App: Free Embeddable Android YouTube Activity!

基本上,播放 YouTube 剪辑的最佳方式是为其创建您自己的活动,这里有一个很好的例子:完善您的应用:免费的可嵌入 Android YouTube 活动!

UPDATE: The problems with the incompatibilities due to YouTube token changes have been fixed. Latest version of the component should work just fine for public YouTube videos.

更新:由于 YouTube 令牌更改导致的不兼容问题已得到修复。该组件的最新版本应该适用于公共 YouTube 视频。

回答by Mark B

You could try switching your website to embed the HTML5 version of the YouTube playerinstead 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.

您可以尝试将您的网站切换为嵌入HTML5 版本的 YouTube 播放器而不是 Flash 版本。仍然不确定这是否会 100% 工作,但在当前不支持 Flash 的设备上,它显然会比 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.

编辑:没关系,看起来 HTML5 版本还需要浏览器支持 H.264 编解码器,目前看起来不像任何 Android 设备都支持。

回答by user2023807

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.

嵌入的 youtube 视频在桌面浏览器和 iPhone 浏览器中运行良好(即使嵌入在 iPhone 上的应用程序中),所以它似乎是 Android 而不是 YouTube 的问题。

回答by Grux

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.

我在 android WebView 中有一个用于 html5 视频标签的库,它适用于所有 youtube 视频和大多数 Flash 视频,支持进入和退出全屏以及其他功能。

VideoEnabledWebView by cprcrack

VideoEnabledWebView 由 cprcrack