2013-11-01 73 views
0

我還是一名Android學員,我開發了一個超過100Mb的應用程序,因此我需要使用擴展文件。Android - 發佈/測試apk與擴展文件 - 它如何工作?

我已經閱讀了數以千計的文件,但我很困惑。

我需要在這種'擴展文件'中壓縮很多mp3文件,以便僅上傳apk代碼而不是整個應用程序+ mp3文件。

我想如果我用所有這些mp3生成一個文件'.obb',我會超過Google Play需要的50MB。

我知道這個'.obb'文件也必須在我的設備的scard/Android/obb文件夾中。

目前我的代碼得到一個MP3文件的「廉政」資源來操縱它是這樣的:

intMyResource = R.raw.name_of_my_music_file;

但是,正如我所說,目前,文件的路徑是'R.raw'。

我的問題:如何更換

intMyResource = R.raw.name_of_my_music_file的最佳方法;

到我的'.obb'文件的實際路徑/名稱?

謝謝大家。

莫羅

回答

0

你應該使用此zip命令創建擴展文件:

zip -r -9 -n ".mp3" main-expansion-file.zip * 

使用-n選項是至關重要的不壓縮的媒體文件,因爲如果媒體文件是壓縮的,你不能用它在你的android應用程序中。 將名稱zip更改爲'main.VERSIONCODE.YOURPACKAGENAME.obb並將其複製到scard/Android/obb文件夾設備中。

閱讀的ZIP擴展文件的文件是與谷歌的ZipFile庫輕鬆的工作(在pathAndroidSDK /額外提供/谷歌/ play_apk_expansion/zip_file)

通過此方法的調用替換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) {} 
      } 

希望這對你有所幫助!