我正在使用Android和PhoneGap应用程序,并使用HTML5视频标记在我的网页上播放视频。当我播放时,视频不可见,视频没有播放。如何在Android上播放HTML5视频?
以下相同的代码
<!DOCTYPE HTML>
<html>
<head>
<script type="text/javascript" charset="utf-8" src="cordova-1.8.1.js"></script>
<meta http-equiv="content-type" content="text/html; charset=">
<title></title>
</head>
<body >
<video id="video" autobuffer height="240" width="360" onclick="this.play();>
<source src="test.mp4">
<source src="test.mp4" type="video/webm">
<source src="test.mp4" type="video/ogg">
</video>
<div id="msg"></div>
<script type="text/javascript">
</script>
</body>
</html>
和活动类onCreate方法 - &gt;&gt;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final WebView webView = (WebView) findViewById(R.id.webview);
WebSettings webSettings = webView.getSettings();
webSettings.setLayoutAlgorithm(LayoutAlgorithm.NARROW_COLUMNS);
webView.getSettings().setJavaScriptEnabled(true);
webSettings.setBuiltInZoomControls(true);
webSettings.setPluginState(PluginState.ON);
webView.getSettings().setPluginsEnabled(true);
webSettings.setAllowFileAccess(true);
webView.loadUrl("file:///android_asset/www/html5videoEvents.html");
}
答案 0 :(得分:2)
在将HTML内容加载到WebView之前添加这些行。
wv.setWebChromeClient(new WebChromeClient() {
});
来自文档:
为了在您的应用程序中支持内联HTML5视频,您需要启用硬件加速,并设置WebChromeClient。对于全屏支持,需要实现onShowCustomView(View,WebChromeClient.CustomViewCallback)和onHideCustomView(),getVideoLoadingProgressView()是可选的。
答案 1 :(得分:0)
试试这个。
MainActivity.java
public class MainActivity extends Activity {
WebView webPage;
Button next;
String rootDir = "file://" + Environment.getExternalStorageDirectory()
+ "/iR-unzip/testbook/";
WebChromeClient webChromeClient = new WebChromeClient();
WebViewClient webViewClient = new WebViewClient();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
webPage = (WebView) findViewById(R.id.webview);
webPage.setWebChromeClient(webChromeClient);
webPage.setWebViewClient(new MyWebViewClient());
webPage.getSettings().setJavaScriptEnabled(true);
webPage.getSettings().setPluginsEnabled(true);
//webPage.getSettings().setPluginState(PluginState.ON);
webPage.getSettings().setLoadWithOverviewMode(true);
webPage.getSettings().setUseWideViewPort(true);
webPage.loadUrl(rootDir + "/" + "chapter_1.html");
next = (Button) findViewById(R.id.page_changer);
next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
webPage.loadUrl(rootDir + "/" + "chapter_2.html");
}
});
}
public class MyWebChromeClient extends WebChromeClient {
@Override
public void onShowCustomView(View view, CustomViewCallback callback) {
// TODO Auto-generated method stub
super.onShowCustomView(view, callback);
if (view instanceof FrameLayout) {
FrameLayout frame = (FrameLayout) view;
if (frame.getFocusedChild() instanceof VideoView) {
VideoView video = (VideoView) frame.getFocusedChild();
frame.removeView(video);
video.start();
}
}
}
}
public class MyWebViewClient extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}