2014-01-23 32 views
1

documentation它說:與APK擴展文件的音頻加載的Soundpool

如果您使用您的擴展文件存儲多媒體文件,ZIP文件 仍允許您使用Android媒體播放調用,提供偏移量和長度控件 (如MediaPlayer.setDataSource()和 SoundPool.load())。

我有10個小的.ogg文件,我想使用SoundPool.load(FileDescriptor fd,long offset,long length,int priority)來加載這些音頻文件。我已經寫了一些代碼,但是當我運行它,我得到錯誤信息:

「無法加載樣品:(空)」

,顯然聲音不播放。這是我試圖:

public class MainActivity extends Activity { 

    SoundPool sounds; 
    boolean loaded = false; 
    int streamID; 
    int theId; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     System.gc(); 
     setContentView(R.layout.activity_main); 
     sounds = new SoundPool(10, AudioManager.STREAM_MUSIC,0); 
     sounds.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() { 
      @Override 
      public void onLoadComplete(SoundPool sp, int sid, int status) { 
       loaded = true; 
     }}); 
     loadSounds(); 
     playSound(theID); 
    } 

    private void loadSounds() { 
     AssetFileDescriptor descriptor = null; 
     try { 
      descriptor = getFileDescriptor(this, "audio_files/end_credits.ogg"); 
     } catch (Exception e) { 
     } finally { 
      if (descriptor != null) 
       try{descriptor.close();} catch (IOException e) {} 
     } 
    theId = sounds.load(descriptor.getFileDescriptor(), descriptor.getStartOffset(), descriptor.getLength(), 1); 
} 
    public static AssetFileDescriptor getFileDescriptor(Context ctx, String path) { 
     AssetFileDescriptor descriptor = null; 
     try { 
      ZipResourceFile zip = APKExpansionSupport.getAPKExpansionZipFile(ctx, 4, -1); 
      descriptor = zip.getAssetFileDescriptor(path); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
     return descriptor; 
    } 
    private void playSound(int soundID){ 
     if(loaded){ 
      streamID = sounds.play(soundID, 1, 1, 1, 0, 1);} 
    } 
} 

回答

0

好吧,似乎是原因是音頻加載速度不夠快。我把playSound(theID);在loadSounds()之後而在播放之前需要幾秒鐘才能加載。但是代碼正在努力達到這一點。