在 Android WebView 中播放 Youtube HTML5 内嵌视频 [英] Play Youtube HTML5 embedded Video in Android WebView

查看:40
本文介绍了在 Android WebView 中播放 Youtube HTML5 内嵌视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 WebView 中运行嵌入的 HTML5 Youtube 时遇到问题.我想在我的应用程序上播放 Youtube 视频.我决定使用 WebView 而不是 VideoView,因为我想让我的系统更灵活地播放来自网络的视频.

I am having a problem running a HTML5 Youtube embedded in a WebView. I want to play a Youtube video on my application. I decided to use WebView instead of VideoView, because I want to make my system more flexible to play video from web.

虽然有很多方法可以在android上播放youtube,但我会使用youtube嵌入式版本.http://www.youtube.com/embed/___________________".因为这是我在您的 android 不支持 flash 时找到的解决方案之一.

Although There are many ways to get play youtube on the android, but I will use the youtube embedded version. "http://www.youtube.com/embed/___________________". Because this is one of the solution I found when your android doesn't support flash.

问题:

WebView 正常加载,包括嵌入的 Youtube.但是一开始我在youtube上看到黑屏.

The WebView load as normal including the embedded Youtube. But I get a black screen on the youtube at start.

当我点击它时.它只加载第一个图像,但它没有播放.我尝试在 android 浏览器上播放,它运行顺利,但在 webView 中运行不正常.

When I click on it. It load the first Image only but then It is not Playing. I tried to play on the android browser, it works smoothly but not in the webView.

知道为什么吗?

以下只是我的代码片段:

Below is just a snip of my code:

WebView wv = new WebView(getApplicationContext());
wv.getSettings().setPluginState(PluginState.ON);
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl("http://apiblog.youtube.com/2010/07/new-way-to-embed-youtube-videos.html");
setContentView(wv);

以下是我认为非常有用的一些资源:

Here are some of the resources that I found very useful:

在 WebView 中播放 YouTube 视频

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

http://apiblog.youtube.com/2010/07/new-way-to-embed-youtube-videos.html

http://www.broken-links.com/2009/10/06/building-html5-video-controls-with-javascript/

http://www.youtube.com/embed/bHQqvYy5KYo

预先感谢您的任何支持和帮助:)

Thank you in advance for any support and help :)

更新(2011 年 6 月 13 日):

我在 WebView 中成功加载了 http://m.youtube.com,但无法播放任何视频.但是当我尝试在我的 Android 浏览器上加载 URL 时,它可以播放.

I successfully load the http://m.youtube.com inside the WebView, but unable to play any video. But When I tried to load the URL on my Android Browser, it can play.

从这里,我注意到来自我的 WebView 的 youtube 站点没有登录.那么我们如何允许 WebView 使用与我手机中的 Youtube 帐户相同的凭据?它真的有效吗?

From here, I notice that the youtube site from my WebView is not signed in. So How can we allow the WebView to use the same credential as my Youtube account in my phone? Will it actually works?

推荐答案

步骤 1 : 只需将此添加到 Manifest 文件中

Step 1 : Simply add this to Manifest file

android:hardwareAccelerated="true"

第 2 步:检查您是否正在为 webview 设置图层.

step 2 : check if you are setting layer for your webview.

(即)//myWebView.setLayerType(View.LAYER_TYPE_SOFTWARE, null);

将硬件加速设置为 true 并评论这些行对我有用.

setting hardware acceleration to true and commenting these lines worked for me .

要了解有关硬件加速和层的更多信息,请访问 http://developer.android.com/guide/topics/graphics/hardware-accel.html

To know more about Hardware Acceleration and Layers look here at http://developer.android.com/guide/topics/graphics/hardware-accel.html

这篇关于在 Android WebView 中播放 Youtube HTML5 内嵌视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆