Blog
How to stream video from a zipped APK resource into VideoView

So you've got a zipped main expansion file (Don't forget to give it a .obb filename).

 

You have a .3gp movie inside that zip file that you want to stream into a VideoView.  You can't pass a path into the VideoView, because it isn't exactly on the storage.  One solution might be to extract the movie, play it then clean up.  Eek.

Our solution is possible using a custom content provider (i.e. a concrete implementation of android.content.ContentProvider).  Most of the detail is available on Android Developer and with a bit of help from Stack Overflow.  In essence, extend com.android.vending.expansion.zipfile.APEZProvider:

    public class ZipContentProvider extends APEZProvider {
    private static final String AUTHORITY = "com.bow.myapp.provider";

    public static Uri buildUri(String pathIntoApk) {
        StringBuilder contentPath = new StringBuilder("content://");

        contentPath.append(AUTHORITY);
        contentPath.append(File.separator);
        contentPath.append(pathIntoApk);

        return Uri.parse(contentPath.toString());
    }

    @Override
    public String getAuthority() {
        return AUTHORITY;
    }
}

and declare the provider in your AndroidManifest:

        <provider android:name=".util.ZipContentProvider"
                  android:authorities="com.bow.myapp.provider" />

 And to stream into a VideoView:

    public void load(String pathIntoApk) {
        _videoView.setVideoURI(ZipContentProvider.BuildUri(pathIntoApk));
    }

Easy, huh?  Well, yes.  Apart from when it doesn't work; we saw it as a logcat error: 

E/MediaPlayer﹕ prepareAsync called in state 1

Debugging through the ZipResourceFile class, we see that getAssetFileDescriptor checks whether the storage method is "kCompressStored", described in the class header as "no compression".  If it is, it passes the opened ParcelFileDescriptor back.  IF IT ISN'T - i.e. it's COMPRESSED in the zip file - it returns null.

So you have to add your video resources uncompressed.  Adding an uncompressed resource into a compressed archive seems somewhat counter-intuitive - especially considering Android Developer recommends "...you use a compressed file to conserve bandwidth...".  

I found IZArc best for adding uncompressed files into the archive: using the Add Files Options (open your zip file, browse to where you want to add the file(s) and select Actions->Add, select the new file(s) and click the Options tab), select the "Archive" checkbox and method "Store".

 

*
*
*
Please read our Code of Conduct, in the site Terms of Use before submitting.
Submit
Ideas realised by creative technologists and independent developers
Bow Software - digitally interactive