如何在 Android 上的 WebView 中嵌入 YouTube 剪辑

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3458765/
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 10:17:10  来源:igfitidea点击:

How to embed a YouTube clip in a WebView on Android

androidhtmliframeyoutube

提问by Simon Hume

Can anyone shed any light on why I'm unable to click and play a youtube clip on an Android handset when placed inside a webview?

任何人都可以解释为什么我无法在放置在 webview 中时在 Android 手机上单击和播放 youtube 剪辑?

I've put a very simple app together (it's being extended, but no right away) and it has a web view assigned using the Android SDK.

我已经把一个非常简单的应用程序放在一起(它正在扩展,但不是马上),它有一个使用 Android SDK 分配的 web 视图。

The rest of the webview works as expected (text, images, css etc), however, the YouTube clips will not show, they just show as black or white rectangles (where the video embed should be).

webview 的其余部分按预期工作(文本、图像、css 等),但是,YouTube 剪辑不会显示,它们只显示为黑色或白色矩形(视频嵌入位置)。

We use the same view to display content on the iPhone app equivalent, without any issues.

我们使用相同的视图在等效的 iPhone 应用程序上显示内容,没有任何问题。

This is using a basic flash embed, the Safari browser on iPhone converts these to H.264 and plays them nicely in QuickTime.

这是使用基本的 Flash 嵌入,iPhone 上的 Safari 浏览器将它们转换为 H.264 并在 QuickTime 中很好地播放。

Can Android do the same??

安卓也可以吗??

Here is the embed code, as an example....

这里是嵌入代码,作为一个例子....

<object width="120" height="73">
    <param name="movie" value="http://www.youtube.com/watch?v=ZVYIBIlTIQs&feature=youtube_gdata"></param>
    <param name="allowFullScreen" value="true"></param>
    <param name="allowscriptaccess" value="always"></param>
    <embed src="http://www.youtube.com/watch?v=ZVYIBIlTIQs&feature=youtube_gdata" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="120" height="73"></embed>
</object>

回答by Sotiris

I am not sure if this is what you need. Anyway I hope the following be useful. You can use the iframe method that youtube provides to play its videos. If the browser supports html5 will show the video with it, otherwise with flash.

我不确定这是否是您所需要的。无论如何,我希望以下内容有用。您可以使用 youtube 提供的 iframe 方法来播放其视频。如果浏览器支持 html5 将显示视频,否则使用 flash。

You can use the following code as an example <iframe class="youtube-player" type="text/html" width="640" height="385" src="http://www.youtube.com/embed/bIPcobKMB94" frameborder="0">

您可以使用以下代码作为示例 <iframe class="youtube-player" type="text/html" width="640" height="385" src="http://www.youtube.com/embed/bIPcobKMB94" frameborder="0">

in the above example the video id is bIPcobKMB94. You can change this id and show your video.

在上面的例子中,视频 ID 是 bIPcobKMB94。您可以更改此 ID 并显示您的视频。

You can access a live example of it here

您可以在此处访问它的实时示例

More infromation for youtube iframe

youtube iframe 的更多信息

YouTube HTML5 Video Player

YouTube HTML5 视频播放器

回答by fredley

Unless the user has Froyo this is going to be impossible. However as long as the user has the YouTube app installed (which is standard for most phones I believe) then you can include a link to the YouTube video, and Android will recognize it as such and open it using the YouTube app. It's not as elegant as embedding, but it's much more reliable.

除非用户有 Froyo,否则这是不可能的。但是,只要用户安装了 YouTube 应用程序(我相信这是大多数手机的标准配置),您就可以包含指向 YouTube 视频的链接,Android 会识别它并使用 YouTube 应用程序打开它。它不像嵌入那样优雅,但更可靠。

回答by Mathias Conradt

You would need to have the flash player, which is only available since Android 2.2 (Froyo).

您需要安装 Flash 播放器,该播放器仅在 Android 2.2 (Froyo) 后可用。