Reputation: 1442
I'm trying to play mp4 video in webView using HTML5 in android but unfortunately it's not working, So can anyone help me, how can i do it ?
Here is my code HTML file with name new2.html
<video width="365" height="200" src="/sdcard/Download/video.mp4" controls autobuffer></video>
<!--<!DOCTYPE html>
<html>
<body>
<video width="320" height="240" controls autoplay>
<source src="/sdcard/Download/video.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</body>
</html>-->
Java file is:
public class WebActivity extends Activity {
WebView wv;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.web_activity);
wv = (WebView) findViewById(R.id.webview);
wv.loadUrl("file:///android_asset/new2.html");
wv.getSettings().setAllowFileAccess(true);
wv.getSettings().setJavaScriptEnabled(true);
wv.getSettings().setPluginsEnabled(true);
// webview.loadUrl("file:///android_asset/new.html");
}
}
XML file is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<WebView
android:id="@+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Upvotes: 12
Views: 10428
Reputation: 1
To play MP4 video in WebView, you need to first declare Content Provider in Manifest
<provider android:name = "MyDataContentProvider" android:authorities="com.myapp" />
Implement open file with open()
method −
URI myURL = URI.create("file:///mypath/new.mp4");
File f = new File(myURL);
ParcelFileDescriptor p =
ParcelFileDescriptor.open(f, ParcelFileDescriptor.MODE_READ_ONLY);
return p;
Upvotes: 0
Reputation: 53
<video width="365" height="200" src="video.mp4" controls autobuffer></video>
and the video.mp4 should be in your assets folder, the same place where you are putting your new2.html
Upvotes: 0
Reputation: 783
What happens if you change "/sdcard/Download/video.mp4" to "file:///sdcard/Download/video.mp4"? I mean the html part:
<video width="365" height="200" src="file:///sdcard/Download/video.mp4" controls autobuffer></video>
I couldn't open the html page in a custom web app. However I noticed that without adding "file:///" the video wouldn't play in my android browser. (In my case by adding "file:///" it took some time before the video started loading.)
Upvotes: 0
Reputation:
From this blog it states there are two steps:
This page shows how to do it as well - a summary from that page follows:
Step 1: Declare your Content Provider in AndroidManifest.xml:
<provider android:name="MyDataContentProvider"
android:authorities="com.techjini" />
^^^^^^^^^^^ change this
Step 2:
Create your ContentProvider
and implement openFile
.
All you have to do is get real path from uri, open it and return the descriptor
URI uri = URI.create("file:///data/data/com.techjini/files/myImage.jpeg");
^^^^^your path to your file
File file = new File(uri);
ParcelFileDescriptor parcel =
ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
return parcel;
You can find out where your image is stored using:
System.out.println(getFilesDir().getAbsolutePath());
You can now load the file:
myWebView.loadUrl("content://com.techjini/myImage.jpeg");
^^^^^^^^^^^change to same as in manifest
hope that helps.
Upvotes: 1
Reputation: 1150
You need to call your file through local host -- file://localhost/ -- otherwise your phone will not recognize that it is you asking for permissions. By using your browser without localhost you are pinging off a satellite and can not make it back on re-rentry.
You are getting the controls to display because they come from a public html folder, but your source ( the video ) does not.
Upvotes: 1