Html 在 android WebView 中启用 HTML5 视频播放?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17259636/
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
Enabling HTML5 video playback in android WebView?
提问by Edmond Tamas
I am building a simple webview application which is now displaying a website filled with short video clips, using the HTML5 video player. Everything runs ok in the default android web browser but webview wont't play any of the video clips.
我正在构建一个简单的 webview 应用程序,它现在使用 HTML5 视频播放器显示一个充满短视频剪辑的网站。在默认的 android web 浏览器中一切正常,但 webview 不会播放任何视频剪辑。
The Html code used to play the video clips is as follows:
用于播放视频片段的Html代码如下:
<video poster preload="true" controls autoplay width="500" height="200">
<source src="http://www.edmondvarga.com/demo/videos/video.mp4" type="video/mp4">
</video>
Main Activity.java :
主要 Activity.java :
package tscolari.mobile_sample;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class InfoSpotActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
WebView mainWebView = (WebView) findViewById(R.id.mainWebView);
WebSettings webSettings = mainWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mainWebView.setWebViewClient(new MyCustomWebViewClient());
mainWebView.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
mainWebView.loadUrl("http://server.info-spot.net");
}
private class MyCustomWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
How could I enable video playback inside webview?
如何在 webview 中启用视频播放?
回答by peterept
I know from a previous project we did that you need to use the WebChromeClient
to get HTML5 video to play. (And this also gives you hardware accelerated support too - providing you also set the flags on your activity).
我从之前的一个项目中了解到,您需要使用WebChromeClient
来播放 HTML5 视频。(这也为您提供了硬件加速支持 - 前提是您还为您的活动设置了标志)。
Use:
用:
mainWebView.setWebChromeClient(new WebChromeClient());
Put that before you set the setWebViewClient. You can override the WebChromeClient to intercept any events you need to handle.
把它放在你设置 setWebViewClient 之前。您可以覆盖 WebChromeClient 以拦截您需要处理的任何事件。
And in your AndroidManifest.xml within your activity definition, add:
在您的活动定义中的 AndroidManifest.xml 中,添加:
android:hardwareAccelerated="true"
The following quote is from this SDK Page(scroll down to HTML5 Video support)
以下引用来自此SDK 页面(向下滚动到 HTML5 视频支持)
In order to support inline HTML5 video in your application, you need to have hardware acceleration turned on, and set a WebChromeClient.
为了在您的应用程序中支持内联 HTML5 视频,您需要打开硬件加速,并设置一个 WebChromeClient。
回答by Mostafizur Rahman
I got success to show video using html5 video tag using the following code:
我使用以下代码使用 html5 视频标签成功显示视频:
mainWebView.setWebChromeClient(new WebChromeClient());
android:hardwareAccelerated="true"