如何使用HTML5在Android的Web视图播放视频 [英] how to play video in Android Web view using html5
问题描述
我用下面的$ C $下扮演机器人的WebView其中包含一个视频,我已经放在了视频和资产文件夹中的海报图像。
vWebview =(web视图)findViewById(R.id.VWebview);
vWebview.getSettings()setJavaScriptEnabled(真)。
vWebview.getSettings()setPluginsEnabled(真)。
ViewContent(生);
InputStream的FILESTREAM = getResources()openRawResource(R.raw.test)。
INT fileLen = fileStream.available();
byte []的fileBuffer =新的字节[fileLen]
fileStream.read(fileBuffer);
fileStream.close();
字符串displayText =新的String(fileBuffer);
vWebview.loadDataWithBaseURL(假货://不是/必要,displayText,text / html的,UTF-8,);
HTML文件是这样的:
<!DOCTYPE HTML>
< HTML的xmlns =http://www.w3.org/1999/xhtml>
< HEAD>
< META HTTP-当量=Content-Type的CONTENT =text / html的;字符集= ISO-8859-1/>
<冠军>的Hello World< /标题>
<身体GT;
< DIV>
&其中p为H.;
<视频SRC =文件:///android_asset/bigbuck.m4v海报=文件:///android_asset/test.jpg的onclick =this.play();/>
&所述; / P>
< / DIV>
< /身体GT;
< / HTML>
这是工作的罚款。海报图像显示正常,只要我点击它给错误:
10月五号至12号:24:22.207:ERROR / HTTP(2882):空或空值的标题主机
十月5日至12日:24:22.207:ERROR / webcoreglue(2882):***从Java调用返回未捕获异常!
十月5日至12日:24:22.262:ERROR / AndroidRuntime(2882):未捕获的处理程序:螺纹WebViewCoreThread退出,由于未捕获的异常
十月5日至12日:24:22.262:ERROR / AndroidRuntime(2882):java.lang.RuntimeException的:空或空值的标题主机
十月5日至12日:24:22.262:ERROR / AndroidRuntime(2882):在android.net.http.Request.addHeader(Request.java:161)
十月5日至12日:24:22.262:ERROR / AndroidRuntime(2882):在android.net.http.Request< INIT>(Request.java:126)
十月5日至12日:24:22.262:ERROR / AndroidRuntime(2882):在android.net.http.RequestQueue.queueRequest(RequestQueue.java:359)
十月5日至12日:24:22.262:ERROR / AndroidRuntime(2882):在android.net.http.RequestQueue.queueRequest(RequestQueue.java:327)
十月5日至12日:24:22.262:ERROR / AndroidRuntime(2882):在android.webkit.HTML5VideoViewProxy $ PosterDownloader.start(HTML5VideoViewProxy.java:275)
十月5日至12日:24:22.262:ERROR / AndroidRuntime(2882):在android.webkit.HTML5VideoViewProxy.loadPoster(HTML5VideoViewProxy.java:490)
十月5日至12日:24:22.262:ERROR / AndroidRuntime(2882):在android.webkit.WebViewCore.nativeTouchUp(本机方法)
十月5日至12日:24:22.262:ERROR / AndroidRuntime(2882):在android.webkit.WebViewCore.access $ 3400(WebViewCore.java:48)
十月5日至12日:24:22.262:ERROR / AndroidRuntime(2882):在android.webkit.WebViewCore $ EventHub $ 1.handleMessage(WebViewCore.java:1095)
十月5日至12日:24:22.262:ERROR / AndroidRuntime(2882):在android.os.Handler.dispatchMessage(Handler.java:99)
十月5日至12日:24:22.262:ERROR / AndroidRuntime(2882):在android.os.Looper.loop(Looper.java:123)
十月5日至12日:24:22.262:ERROR / AndroidRuntime(2882):在android.webkit.WebViewCore $ WebCoreThread.run(WebViewCore.java:612)
十月5日至12日:24:22.262:ERROR / AndroidRuntime(2882):在java.lang.Thread.run(Thread.java:1096)
我下面code放置在default.html文件的RAW文件夹和访问视频文件,视频文件的方式:
video.src =android.resource:// ProjectPackageAame /生/ bigbuck;
video.type =视频/ MP4;
video.load();
video.play();
据playes视频像我想要的。还增加了以下的AndroidManifest文件中的行。
安卓hardwareAccelerated =真
试试这个 - <一个href="http://stackoverflow.com/questions/17020270/where-to-place-local-video-files-for-android-webview-html5-hybrid-app">where把本地视频文件,对于Android的WebView HTML5混合应用
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)
I placed video files in RAW folder and access video file in default.html file by following code:
video.src ="android.resource://ProjectPackageAame/raw/bigbuck";
video.type = "video/mp4";
video.load();
video.play();
It playes video like i want. Also added following line in AndroidManifest file.
android:hardwareAccelerated="true"
Try this - where to place local video files for android webview html5 hybrid app
这篇关于如何使用HTML5在Android的Web视图播放视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!