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!