你應該使用此zip命令創建擴展文件:
zip -r -9 -n ".mp3" main-expansion-file.zip *
使用-n選項是至關重要的不壓縮的媒體文件,因爲如果媒體文件是壓縮的,你不能用它在你的android應用程序中。 將名稱zip更改爲'main.VERSIONCODE.YOURPACKAGENAME.obb並將其複製到scard/Android/obb文件夾設備中。
閱讀的ZIP擴展文件的文件是與谷歌的ZipFile庫輕鬆的工作(在pathAndroidSDK /額外提供/谷歌/ play_apk_expansion/zip_file)
通過此方法的調用替換R.raw.name_music_file:
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;
}
示例代碼從擴展文件播放音樂的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) {}
}
希望這對你有所幫助!