从扩展文件中读取mp3

Pra*_*rge 6 mp3 android file expansion

从扩展文件中读取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


}
Run Code Online (Sandbox Code Playgroud)

但它总是在这条线上抛出异常

 mediaPlayer.setDataSource( fd.getFileDescriptor(), 
                    fd.getStartOffset(),fd.getLength());
            mediaPlayer.prepare();
Run Code Online (Sandbox Code Playgroud)

因为变量fd为null.

任何人都可以帮助我解决这个问题

San*_*ela 7

我用Google搜索,发现我们shold不得不作出的.zip用0% (No compression)即提http://developer.android.com/google/play/expansion-files.html

Tip: If you're packaging media files into a ZIP, you can use media playback calls on the files with offset and length controls (such as MediaPlayer.setDataSource() and SoundPool.load()) without the need to unpack your ZIP. In order for this to work, you must not perform additional compression on the media files when creating the ZIP packages. For example, when using the zip tool, you should use the -n option to specify the file suffixes that should not be compressed: zip -n .mp4;.ogg main_expansion media_files

那么如何使用winrar制作0%压缩拉链? 在此输入图像描述

这里看压缩方法

所以我们应该在Play商店上传这个zip.

所以你不需要使用其他SO答案中提到的ZipHelper.java

只是简单地使用

ZipResourceFile expansionFile=null;

            try {
                expansionFile = APKExpansionSupport.getAPKExpansionZipFile(getApplicationContext(),3,0);

                     AssetFileDescriptor fd = expansionFile.getAssetFileDescriptor("test.mp3");
                     MediaPlayer mPlayer = new MediaPlayer();
                     mPlayer.setAudioStreamType(AudioManager.STREAM_MUSIC);
                     mPlayer.setDataSource(fd.getFileDescriptor(),fd.getStartOffset(),fd.getLength());
                     mPlayer.prepare();
                     mPlayer.start();

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
Run Code Online (Sandbox Code Playgroud)