简体   繁体   English

如何更改我的代码以使用APK扩展文件?

[英]How do I change my code to use APK Expansion file?

My application is over 50MB because I use a lot of MP3 files. 我的应用程序超过50MB,因为我使用了很多MP3文件。 I typical do this ... 我通常这样做...

uri = Uri.parse("android.resource://red.cricket.RecordAlbum/" + R.raw.track_001_64kb );

... but if I have to use APK Expansion file I assume the above line of code has to change. ...但是如果我必须使用APK扩展文件,我认为上面的代码行必须更改。 But change to what? 但是变成什么呢? Any help is greatly appreciated! 任何帮助是极大的赞赏!

Edit 1) Thanks for your answer Ted. 编辑1)感谢您的回答泰德。 I did create the expansion file like so: 我确实是这样创建扩展文件的:

red@ubuntu:~/android/workspace/Album/res/raw$ zip -n .mp3 main.1.com.redcricket.Album.obb *mp3

So what do I do now? 那我现在该怎么办? rm -f *mp3; cd ../..; ant clean ; ant debug rm -f *mp3; cd ../..; ant clean ; ant debug Somehow I do not think that is going to work. rm -f *mp3; cd ../..; ant clean ; ant debug不知何故我认为这行不通。 Where do I put the main.1.com.redcricket.Album.obb file? 我应该将main.1.com.redcricket.Album.obb文件放在哪里?

The steps are pretty clearly laid out in the docs . 这些步骤在文档中已明确列出。 In outline, the steps are: 概括而言,这些步骤是:

  1. Remove the .mp3 files from your .apk and put them in an expansion file. 从您的.apk中删除.mp3文件,并将其放入扩展文件中。 Name the expansion file according to the directions. 根据说明命名扩展文件。 The format of the file can be anything you want, but the docs suggest that for audio files you use an uncompressed ZIP format. 该文件的格式可以是您想要的任何格式,但是文档建议您为音频文件使用未压缩的ZIP格式。 Android includes the APK expansion ZIP library that can be used to load .mp3 files directly from a ZIP file without having to unpack it. Android包含APK扩展ZIP库 ,可用于直接从ZIP文件加载.mp3文件,而无需将其解压缩。
  2. Rewrite the app to look for the expansion file in the location 重写应用以在该位置查找扩展文件

     Context ctx = getContext(); String loc = ctx.getExternalStorageDirectory() + "/Android/obb/" + ctx.getPackageName(); 
  3. Write code so that if the file is not there, your app will download it from Google Play, using the Application Licensing service as described in the docs. 编写代码,以便如果文件不存在,您的应用程序将使用文档中所述的应用程序许可服务从Google Play下载文件。 There's quite a bit of coding and configuration involved in this step. 此步骤涉及很多编码和配置。

  4. Access the .mp3 file(s) you want using the APK expansion ZIP library. 使用APK扩展ZIP库访问所需的.mp3文件。 The docs have examples of loading an MP3 from an expansion file by calling MediaPlayer.setDataSource() with a file descriptor, an offset into the ZIP file, and a file length. 该文档提供了通过调用MediaPlayer.setDataSource() ,文件描述符,ZIP文件的偏移量和文件长度来从扩展文件中加载MP3的示例。

Also see this thread for more details on how to do all this. 另请参阅此线程以获取有关如何执行所有操作的更多详细信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM