6

拡張ファイルからの 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 が null であるためです。

これを解決するのを手伝ってくれる体はありますか

4

3 に答える 3

1

使ってみてくださいexpansionFile.getAssetFileDescriptor("main_expansion/about_eng.mp3")

うまくいかない場合は、次のように呼び出します。

ZipResourceFile.ZipEntryRO [] entries = expansionFile.getAllEntries();

ファイル名を見てください:

entries[0].mFileName;.

これは、パスがどのように見えるかのヒントになる可能性があります。

質問が古いことは知っていますが、誰かの助けになるかもしれません。

于 2017-02-09T13:14:32.113 に答える