Nirav Modh
Nirav Modh

Reputation: 787

Android Expansion file.

On my Application I have lots of media file(.mp4) and pdf file so i have upload apk file with expansion .

but I have no idea how to play video file to Video view from expansion file(obb file) and open PDF file from expansion file .

anybody have idea about. please help me

Upvotes: 1

Views: 789

Answers (2)

Sapan Diwakar
Sapan Diwakar

Reputation: 10966

You can use the APKExpansionSupport class to obtain reference to the expansion file and then open the asset from file.

// Get a ZipResourceFile representing a merger of both the main and patch files
try {  
    ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(this, 2, 0);
    AssetFileDescriptor afd = expansionFile.getAssetFileDescriptor("path-to-music-from-expansion.mp3");

    try {
        mMediaPlayer.setDataSource(afd.getFileDescriptor(), afd.getStartOffset(), afd.getDeclaredLength());
    } catch (IllegalArgumentException | IllegalStateException | IOException e) {
        Log.w(TAG, "Failed to update data source for media player", e);
    }

    try {
        mMediaPlayer.prepareAsync();
    } catch (IllegalStateException e) {
        Log.w(TAG, "Failed to prepare media player", e);
    }
    mState = State.Preparing;

    try {
        afd.close();
    } catch (IOException e) {
        Log.d(TAG, "Failed to close asset file descriptor", e);
    }
} catch (IOException e) {
    Log.w(TAG, "Failed to find expansion file", e);
}

If you're using your expansion files to store media files, a ZIP file still allows you to use Android media playback calls that provide offset and length controls (such as MediaPlayer.setDataSource() and SoundPool.load()). In order for this to work, you must not perform additional compression on the media files when creating the ZIP packages. For example, when using the zip tool, you should use the -n option to specify the file suffixes that should not be compressed:

zip -n .mp4;.ogg main_expansion media_files

For more details and code for setting up expansion files, read How to Set Up Android App to Support Expansion Files.

Upvotes: 1

Jordy
Jordy

Reputation: 1804

Its actually quite easy to read movie files and other stuff directly from the expansion file using the zip tools that google has provided (com.android.vending.zipfile).

First get the expansionfile using the methods provided in the library, the paremeters are integers that represent your main expansion apk version (the apk version where the expansion pack you need was first added) and the patch apk version.

ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(context, APKX_MAIN_APK, APKX_PATCH_APK);

Video

For playing video directly from this zipresourcefile:

AssetFileDescriptor a = expansionFile.getAssetFileDescriptor(pathToFileInsideZip);

Now from this assetFileDescriptor you can get a FileDescriptor and use this in your mediaplayer, the correct syntax to get your mediaplayer to play the video also needs the second and third parameter.. Be it the startoffset and length you can get from the AssetFileDescriptor.

player.setDataSource(a.getFileDescriptor(), a.getStartOffset(), a.getLength());

Other

For all the other stuff (like images) you can just get an inputstream of the zipresourcefile:

expansionFile.getInputStream(pathToFileInsideZip);`

ALSO make sure you don't compress the videos in the zip for this to work! for example not to compress .mp4 files:

zip -n .mp4 -r zipfile.zip . -x ".*" -x "*/.*"

Upvotes: 0

Related Questions