android - 如何使用 html5 在 Android Web View 中播放视频

标签 android html webview html5-video

我使用下面的代码在包含 的 android WebView 中播放视频,我将视频和海报图像放在 Assets 文件夹中。

vWebview = (WebView)findViewById(R.id.VWebview);
vWebview.getSettings().setJavaScriptEnabled(true);
vWebview.getSettings().setPluginsEnabled(true);

ViewContent(raw);   


InputStream fileStream = getResources().openRawResource(R.raw.test); 
int fileLen = fileStream.available();
byte[] fileBuffer = new byte[fileLen]; 
fileStream.read(fileBuffer); 
fileStream.close(); 
String displayText = new String(fileBuffer);
vWebview.loadDataWithBaseURL("fake://not/needed", displayText, "text/html", "utf-8", "");

html文件是这样的:

<!DOCTYPE HTML>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Hello World</title>


<body>
<div>

<p>
<video src="file:///android_asset/bigbuck.m4v" poster="file:///android_asset/test.jpg" onclick="this.play();"/>
</p>

</div>
</body>
</html>

它工作正常。海报图片显示正确,我一点击它就出现错误:

05-12 10:24:22.207: ERROR/http(2882): Null or empty value for header "Host"
05-12 10:24:22.207: ERROR/webcoreglue(2882): *** Uncaught exception returned from Java call!
05-12 10:24:22.262: ERROR/AndroidRuntime(2882): Uncaught handler: thread WebViewCoreThread exiting due to uncaught exception
05-12 10:24:22.262: ERROR/AndroidRuntime(2882): java.lang.RuntimeException: Null or empty value for header "Host"
05-12 10:24:22.262: ERROR/AndroidRuntime(2882):     at android.net.http.Request.addHeader(Request.java:161)
05-12 10:24:22.262: ERROR/AndroidRuntime(2882):     at android.net.http.Request.<init>(Request.java:126)
05-12 10:24:22.262: ERROR/AndroidRuntime(2882):     at android.net.http.RequestQueue.queueRequest(RequestQueue.java:359)
05-12 10:24:22.262: ERROR/AndroidRuntime(2882):     at android.net.http.RequestQueue.queueRequest(RequestQueue.java:327)
05-12 10:24:22.262: ERROR/AndroidRuntime(2882):     at android.webkit.HTML5VideoViewProxy$PosterDownloader.start(HTML5VideoViewProxy.java:275)
05-12 10:24:22.262: ERROR/AndroidRuntime(2882):     at android.webkit.HTML5VideoViewProxy.loadPoster(HTML5VideoViewProxy.java:490)
05-12 10:24:22.262: ERROR/AndroidRuntime(2882):     at android.webkit.WebViewCore.nativeTouchUp(Native Method)
05-12 10:24:22.262: ERROR/AndroidRuntime(2882):     at android.webkit.WebViewCore.access$3400(WebViewCore.java:48)
05-12 10:24:22.262: ERROR/AndroidRuntime(2882):     at android.webkit.WebViewCore$EventHub$1.handleMessage(WebViewCore.java:1095)
05-12 10:24:22.262: ERROR/AndroidRuntime(2882):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-12 10:24:22.262: ERROR/AndroidRuntime(2882):     at android.os.Looper.loop(Looper.java:123)
05-12 10:24:22.262: ERROR/AndroidRuntime(2882):     at android.webkit.WebViewCore$WebCoreThread.run(WebViewCore.java:612)
05-12 10:24:22.262: ERROR/AndroidRuntime(2882):     at java.lang.Thread.run(Thread.java:1096)

最佳答案

我将视频文件放在 RAW 文件夹中,并通过以下代码访问 default.html 文件中的视频文件:

  video.src ="android.resource://ProjectPackageAame/raw/bigbuck";
  video.type = "video/mp4";      
  video.load(); 
  video.play();

它按我想要的方式播放视频。还在 AndroidManifest 文件中添加了以下行。

  android:hardwareAccelerated="true"

试试这个 - where to place local video files for android webview html5 hybrid app

关于android - 如何使用 html5 在 Android Web View 中播放视频,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5865946/

相关文章:

android - 将 View 转换为位图而不在 Android 中显示?

javascript - 更改png图像背景颜色

objective-c - cocoa WebView : can I load web and local resources together in the same page?

android - 如何检索 AndroidManifest.xml 文件?

android - 如何检查协程是否使用相同的上下文?

Android Sqlite 添加列后根据条件选择条目

javascript - 用 canvas/JS 高亮图像区域

html - 在页面的多个位置上使用相同的 itemprop 和 itemscope itemtype 可以吗

android webview - 设置引用(对于版本 < 2.2 aka Froyo)

android - android webview 上一页的后退按钮不起作用