如何使用 html5 在 Android Web 视图中播放视频
我使用下面的代码在 android WebView 中播放视频,其中包含一个 ,我已将视频和海报图像放置在 asset 文件夹中。
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)
I was used the below code for playing video in android WebView which contains a , I have placed the the video and the poster image in asset folder.
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", "");
The html file is like that:
<!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>
It is working fine. the poster image is showing properly, as soon as I am clicking it is giving error:
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)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我将视频文件放在 RAW 文件夹中,并通过以下代码访问 default.html 文件中的视频文件:
它按照我想要的方式播放视频。还在 AndroidManifest 文件中添加了以下行。
试试这个 - 本地视频的放置位置Android webview html5 混合应用程序的文件
I placed video files in RAW folder and access video file in default.html file by following code:
It playes video like i want. Also added following line in AndroidManifest file.
Try this - where to place local video files for android webview html5 hybrid app
根据 http://html5test.com/compare/browser/android40.html,Android 4.0 支持视频元素,但不支持嵌入 MP4、H.264 或 Ogg 视频。我还尝试使用 HTMl5 视频创建 PhoneGap Android 应用程序,但它无法播放视频。 HTML5有效,但Android浏览器无法播放视频。另外,您的 HTML5 视频代码似乎有误。 W3Schools 网站的 HTML5
video
标签设置如下:浏览器如何在不支持任何视频类型的情况下支持 HTML5 视频标签,这确实令人困惑。
According to http://html5test.com/compare/browser/android40.html, Android 4.0 supports the video element, but it doesn't support embedding MP4, H.264, or Ogg videos. I have also tried to create a PhoneGap Android app with HTMl5 video, and it wouldn't play the video. The HTML5 is valid, but the Android browser cannot play the videos. Also, your HTML5 video code seems wrong. The W3Schools website has the HTML5
video
tag setup like this:It's really confusing how the browser can support the HTML5 video tag without supporting any video types.
从视频标签中删除 src 属性,并使用 javascript 函数来播放视频。
Remove the src atribute from the video tag and make javascript function to play the video.