读MP3的扩展文件 [英] Reading mp3 from expansion file

查看:178
本文介绍了读MP3的扩展文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从扩展文件中读取MP3文件

Reading mp3 file from expansion file

我已创建和扩展名文件

main.1.com.example.app.obb

"main.1.com.example.app.obb"

,其中包含音频文件名about_eng.mp3

which contains and audio file name "about_eng.mp3"

现在我已经写了下面的code读取并播放MP3文件的问题

Now the issue i have written the following code to read and play the mp3 file

    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


}

但它始终是在该行抛出异常

But it is always throwing exception at this line

 mediaPlayer.setDataSource( fd.getFileDescriptor(), 
                    fd.getStartOffset(),fd.getLength());
            mediaPlayer.prepare();

因为变量fd是空。

because the variable fd is null.

任何机构可以帮我解决这个

Can any body help me to solve this

推荐答案

我用Google搜索,发现我们shold不得不作出的.zip与 0%(没有COM pression)即提及<一href="http://developer.android.com/google/play/expansion-files.html">http://developer.android.com/google/play/expansion-files.html

i have googled and found that we shold have to make .zip with 0% (No compression) that is mention in http://developer.android.com/google/play/expansion-files.html

提示:如果你在打包的媒体文件成ZIP,你可以使用这些文件的媒体播放与通话偏移量和长度的控制(如MediaPlayer.setDataSource()和SoundPool.load() )无需解压您的邮递。为了这个工作,你不能创建ZIP包时,执行上的媒体文件的其他COM pression。例如,使用压缩工具时,你应该使用-n选项来指定文件后缀不应该是COM pressed: 拉链-n .MP4; .OGG main_expansion media_files

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%COM pression拉链?

so How to make 0% compression zip using winrar?

在这里看到的COM pression方法

here see the compression method

所以我们应该要上传这个压缩在游戏商店。

so we should have to upload this zip in play store.

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

so you not need to use ZipHelper.java that is mentioned in other SO answer

只是简单地使用

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();
            }

这篇关于读MP3的扩展文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆