How to play video in webview by html 5

I have a problem, I cannot play video in webview by html5 here, my code

webView = (WebView) findViewById(R.id.web);//new WebView(this); webView.setBackgroundColor(android.R.color.transparent); webView.getSettings().setJavaScriptEnabled(true); webView.getSettings().setPluginsEnabled(true); webView.getSettings().setAllowFileAccess(true); webView.getSettings().setDefaultZoom(WebSettings.ZoomDensity.FAR); webView.getSettings().setLayoutAlgorithm(WebSettings.LayoutAlgorithm.NARROW_COLUMNS); webView.loadUrl("file:///android_asset/index.html"); 

and here is mycode html

  <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <video autoplay="autoplay" controls="controls" style="align:center;" autobuffer onclick="this.play();"> <source src="birtday2527.theora.ogv" type="video/ogg" codecs="theora, vorbis"/> <source src="birtday2527.mp4" type="video/mp4"/> </video> </body> 

I have a link to the test.

+1
source share
2 answers
 Webview = (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", ""); 

and this

 <!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/test.m4v" poster="file:///android_asset/test.jpg" onclick="this.play();"/> </p> </div> </body> </html> 
+1
source

I think that the Webkit video player just starts the intention of the video player, you can call it directly with the intention ACTION_VIEW with the type "video / *". For instance:

  Intent newIntent = new Intent(android.content.Intent.ACTION_VIEW); newIntent.setDataAndType(Uri.fromFile(file),mimeType); newIntent.setFlags(newIntent.FLAG_ACTIVITY_NEW_TASK); try { context.startActivity(newIntent); } catch (android.content.ActivityNotFoundException e) { Toast.makeText(_context, "No app for this type of file.", 4000).show(); } 
0
source

Source: https://habr.com/ru/post/1492660/


All Articles