【问题标题】:Android - Publish/Test apk with expansion files - How it does work?Android - 使用扩展文件发布/测试 apk - 它是如何工作的?
【发布时间】:2013-11-12 19:34:38
【问题描述】:

我还是一名 Android 学生,我已经开发了一个超过 100Mb 的应用程序,因此我需要使用扩展文件。

我已经阅读了成千上万的文件,但我很困惑。

我需要在那种“扩展文件”中压缩许多 mp3 文件,以便只上传 apk 代码而不是整个 app+mp3 文件。

我想如果我用所有这些 mp3 文件生成一个文件“.obb”,我会超过 Google Play 要求的 50MB。

我知道这个“.obb”文件也必须在我设备的 scard/Android/obb 文件夹中。

目前我的代码从 mp3 文件中获取“int”资源来操作它是这样的:

intMyResource=R.raw.name_of_my_music_file;

但是,目前,正如我所说,文件的路径是“R.raw”。

我的问题:最好的替换方法是什么

intMyResource=R.raw.name_of_my_music_file;

到我的“.obb”文件所在的实际路径/名称?

谢谢大家。

毛罗

【问题讨论】:

    标签: android expansion-files


    【解决方案1】:

    您应该使用此 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) {}
                }
    

    希望对您有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-03
      • 1970-01-01
      • 1970-01-01
      • 2015-08-09
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      • 2021-09-03
      相关资源
      最近更新 更多