My goal:
Download a zip file that contains a video and JS file. Create a webview that runs the JS file which (among other things) contains a video tag to play that video file.
Problem: when I try to play the video I get an error saying "Sorry this video cannot be played". In logcat I get: error( 1, -2147483648)
Here is the code to unzip the files:
String path = context.getFilesDir().getPath()+"/";
InputStream is;
ZipInputStream zis;
try {
is = new FileInputStream(zip file);
zis = new ZipInputStream( new BufferedInputStream(is));
ZipEntry ze = null;
while ((ze = zis.getNextEntry()) != null) {
String filename = ze.getName();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int count;
FileOutputStream fout =
_context.openFileOutput( path + filename, Context.MODE_WORLD_READABLE );
while ((count = zis.read(buffer)) != -1) {
baos.write(buffer, 0, count);
baos.toByteArray();
fout.write(baos.toByteArray());
baos.reset();
}
fout.close();
zis.closeEntry();
baos.close();
}
zis.close();
}
To show the video I override MraidWebChromeClient.onShowCustomView:
super.onShowCustomView(view, callback);
if (view instanceof FrameLayout) {
FrameLayout frame = (FrameLayout) view;
if (frame.getFocusedChild() instanceof VideoView) {
VideoView video = (VideoView) frame.getFocusedChild();
frame.removeView(video);
Activity a = (Activity)getContext();
a.setContentView(video);
video.setOnCompletionListener(this);
video.setOnErrorListener(this);
video.start();
}
}
I do not believe there is an error with the video file, pathing or JS because:
- The video plays fine if included as a resource in res or streamed with an external http link.
- when loading the js file I use loadDataWithBaseURL and all other image elements show up fine.
- I have copied the file from res to the local app folder (using similar code to the unzipping code) and the same error occurs.
I am thinking either:
- the file is being corrupted while its being unzipped/copied
- there is a permissions issue for playing a local video from a webview (even after I've set the file to world_readable. (from this link WebView NOT opening android default video player?)
Any insights into this issue would be greatly appreciated!
K
Ps: does anyone know why in the normal web-browser it will download .mp4 links while in other cases it will try and stream them?
EDIT: After researching these two post seem to suggest that Android has security to prevent this: Android - Load video from private folder of app Playing an app local video (.mp4) in a webview Copying files to public external worked fine.
And with regards to the Ps: why some videos stream and some are downloaded, Android 3 doesn't support streaming from https and so will download the file.