在 Android WebView 中嵌入来自 vimeo 的视频
我最近创建了一个移动网站,其中嵌入了多个来自 Vimeo 的 iframe。现在,在带有 Flash 的 Android 浏览器中,这工作得很好,但是当我尝试将其塞入伪应用程序的 WebView 中时,视频将无法播放。我已经阅读了很多关于这个问题的帖子,这是我到目前为止得到的代码:
WebView wv = (WebView) findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);
if (Build.VERSION.SDK_INT < 8) {
wv.getSettings().setPluginsEnabled(true);
} else {
wv.getSettings().setPluginState(PluginState.ON);
}
wv.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
wv.loadUrl("myurl.here");
页面加载正常,除了视频播放之外,一切正常。我得到的只是一个黑色矩形,不响应任何内容。
我尝试播放的视频 iframe 看起来都是这样的:
我也尝试过启用硬件加速应用程序,但这会导致一个全新的问题。启动应用程序后,显示的全部内容如下(在 Galaxy Nexus 上测试): https://i.sstatic.net/RJyHQ.png
手机上已安装 Flash,并且通过浏览器正常加载网页效果很好。
我一整天都在谷歌上搜索这个问题,但我仍然像我开始试图弄清楚它时一样陷入困境。我开始认为这根本不可能。
更新:借了一台运行android 3.2的xoom平板电脑,视频在启用硬件加速的情况下可以正常播放。不过,我仍然在连接上遇到奇怪的渲染问题,所以它并不能完全解决问题。
I've recently created a mobile web site in which there are several iframe embeds from Vimeo. Now, in the android browser with flash this works pretty well, but when I try to cram it into a WebView for a pseudo-app, the videos won't play. I've read a lot of threads on the issue and this is the code I've got so far:
WebView wv = (WebView) findViewById(R.id.webview);
wv.getSettings().setJavaScriptEnabled(true);
if (Build.VERSION.SDK_INT < 8) {
wv.getSettings().setPluginsEnabled(true);
} else {
wv.getSettings().setPluginState(PluginState.ON);
}
wv.setWebViewClient(new WebViewClient(){
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
return false;
}
});
wv.loadUrl("myurl.here");
The page loads fine and everything works, except for the video playback. All I get is a black rectangle that doesn't respond to anything.
The video iframes I'm trying to play all look like this: <iframe src='http://player.vimeo.com/video/<VIDEO ID HERE>?title=0&byline=0&portrait=0' width='100%' height='auto' frameborder='0'></iframe>
I've also tried enabling hardware acceleration for the app, but this causes an entirely new problem. Upon starting the app this is all that shows (tested on a galaxy nexus):
https://i.sstatic.net/RJyHQ.png
Flash is installed on the phone, and loading the web page normally through the browser works fine.
I've been googling this all day and I'm still just as stuck as I was when I started trying to figure it out. I'm starting to think this isn't possible at all.
UPDATE: Borrowed a xoom tablet running android 3.2, the video plays fine with hardware acceleration enabled. I still get the weird rendering thing on the nexus though, so it doesn't fix the issue completely.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果有人发现这个,我在此处找到了一个可能有帮助的答案。
您必须在加载 WebView 之前添加此代码:
用法示例:
并确保
在 AndroidManifest.xml 的
标记中包含:(仅适用于 API 11 或更高版本)In case anyone finds this, I found an answer that might help here.
You have to add this code before you load the WebView:
Example usage:
And make sure you include:
in the
<manifest>
tag of AndroidManifest.xml (only applies to API 11 or higher)