您应该使用此 zip 命令创建扩展文件:
zip -r -9 -n ".mp3" main-expansion-file.zip *
使用 -n 选项对于不压缩媒体文件至关重要,因为如果媒体文件被压缩,您将无法在您的 android 应用程序中使用它。
将名称 zip 更改为 'main.VERSIONCODE.YOURPACKAGENAME.obb 并将此 .obb 文件复制到 scard/Android/obb 文件夹设备中。
使用 Google ZipFile 库(在 pathAndroidSDK/extras/google/play_apk_expansion/zip_file 中提供)可以轻松读取 zip 扩展文件的文件
通过调用此方法替换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) {}
}
希望对您有所帮助!