0

I'm still a Android student and I have develop an application which has more than 100Mb and therefore I need to use expansion files.

I have read thousands of documents, but I am very confused.

There are many mp3 files I need to compact in that kind of 'expansion file', in order to upload only apk code and not the entire app+mp3 files.

I think if I generate a file '.obb' with all those mp3 ones, I would go over 50MB requrired by Google Play.

I know this '.obb' file must be in scard/Android/obb folder of my device, too.

Currently my code get the 'int' resource from a mp3 file to manipulate it is like this:

intMyResource=R.raw.name_of_my_music_file;

But, currently, as I told, the path of the files is 'R.raw'.

My question: How is the best method to replace

intMyResource=R.raw.name_of_my_music_file;

to the actual path/name where my '.obb' file is?

Thank you all.

Mauro

1 Answers1

0

You should create expansion file with this zip command:

zip -r -9 -n ".mp3" main-expansion-file.zip *

Use -n option is critical to don't compress media files, because if media files are compressed you cannot use it in your android application. Change name zip to 'main.VERSIONCODE.YOURPACKAGENAME.obb and copy this .obb file in scard/Android/obb folder device.

Read files of zip expansion files is easy work with Google ZipFile Library (available in pathAndroidSDK/extras/google/play_apk_expansion/zip_file)

Replace R.raw.name_music_file by call of this method:

public static AssetFileDescriptor getFileDescriptor(Context ctx, String path) {
        AssetFileDescriptor descriptor = null;
        try {
            ZipResourceFile zip = APKExpansionSupport.getAPKExpansionZipFile(ctx, 1, -1);
            descriptor = zip.getAssetFileDescriptor(path);
        } catch (IOException e) {
            Log.e("APKExpansionSupport", "ERROR: " + e.getMessage(), e);
            e.printStackTrace();
        }
        return descriptor;
    }

Example code to play music from expansion file with MediaPlayer:

    AssetFileDescriptor descriptor = null;
            try {
                descriptor = getFileDescriptor(this, "name_music_file.mp3"));
                MediaPlayer reproductor = new MediaPlayer();
                reproductor.setDataSource(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength());
                reproductor.setOnCompletionListener(this);
                reproductor.prepare();
                reproductor.start();

            } catch (Exception e) {
                Log.e("Play mp3", "ERROR: " + e.getMessage(), e);
            } finally {
                if (descriptor != null)
                    try{descriptor.close();} catch (IOException e) {}
            }

Hope this help you!