【发布时间】:2012-10-23 13:13:18
【问题描述】:
从扩展文件中读取 mp3 文件
我已经创建和扩展文件名
“main.1.com.example.app.obb”
其中包含音频文件名“about_eng.mp3”
现在的问题是我写了以下代码来读取和播放 mp3 文件
private final static String EXP_PATH = "/Android/obb/";
static String[] getAPKExpansionFiles(Context ctx, int mainVersion, int patchVersion) {
String packageName = ctx.getPackageName();
Vector<String> ret = new Vector<String>();
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
// Build the full path to the app's expansion files
File root = Environment.getExternalStorageDirectory();
File expPath = new File(root.toString() + EXP_PATH + packageName);
// Check that expansion file path exists
if (expPath.exists()) {
if ( mainVersion > 0 ) {
String strMainPath = expPath + File.separator + "main." +
mainVersion + "." + packageName + ".obb";
File main = new File(strMainPath);
if ( main.isFile() ) {
ret.add(strMainPath);
}
}
if ( patchVersion > 0 ) {
String strPatchPath = expPath + File.separator + "patch." +
mainVersion + "." + packageName + ".obb";
File main = new File(strPatchPath);
if ( main.isFile() ) {
ret.add(strPatchPath);
}
}
}
}
String[] retArray = new String[ret.size()];
ret.toArray(retArray);
return retArray;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
// Get a ZipResourceFile representing a merger of both the main and patch files
try {
ZipResourceFile expansionFile = APKExpansionSupport.getAPKExpansionZipFile(this,1,1);
if(expansionFile!=null){
AssetFileDescriptor fd = expansionFile.getAssetFileDescriptor("about_eng.mp3");
//or
MediaPlayer mediaPlayer = new MediaPlayer();
mediaPlayer.setDataSource( fd.getFileDescriptor(),
fd.getStartOffset(),fd.getLength());
mediaPlayer.prepare();
mediaPlayer.start();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Get an input stream for a known file inside the expansion file ZIPs
}
但它总是在这一行抛出异常
mediaPlayer.setDataSource( fd.getFileDescriptor(),
fd.getStartOffset(),fd.getLength());
mediaPlayer.prepare();
因为变量 fd 为空。
谁能帮我解决这个问题
【问题讨论】:
-
我觉得这篇文章可以帮到你:stackoverflow.com/questions/12635069/…
-
检查您的 zip 文件是否未压缩。如果 zip 被压缩,“getAssetFileDescriptor”返回 null
-
你解决了吗?我面临同样的问题stackoverflow.com/questions/14495483/…
标签: android file mp3 expansion