Soundpool.builder没有准备就绪

时间:2015-04-13 08:25:44

标签: android soundpool

我在soundpool.builder中加载声音样本时遇到了问题,同时在弃用的soundpool中声音完美。我的代码:

 public void createNewSoundPool(Context context, int value){
   AudioAttributes attributes = new AudioAttributes.Builder()
           .setUsage(AudioAttributes.USAGE_MEDIA)
           .setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION)
           .build();
    soundPool = new SoundPool.Builder()
           .setAudioAttributes(attributes)
            .setMaxStreams(10)
           .build();
    if(value==1) {
       soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() {               @Override
            public void onLoadComplete(SoundPool soundPool, int sampleId, int status) {
                loaded= true;
            }
       });
        soundIds[0] = soundPool.load(context, R.raw.one, 1);
        soundIds[5] = soundPool.load(context, R.raw.stick, 6);
        soundIds[3] = soundPool.load(context, R.raw.loss, 4);
        soundIds[4] = soundPool.load(context, R.raw.win, 5);
    }

 if(loaded==true) {
        if (value == 1) {
            soundPool.play(soundIds[0], 1, 1, 1, 0, 1);
        }

     if(value==2){
        soundPool.play(soundIds[1],1,1,1,0,1);
    }else if(value==3){
        soundPool.play(soundIds[2],1,1,1,0,1);
    }else if(value==4){
        soundPool.play(soundIds[3],1,1,1,0,1);
    }else if(value==5){
        soundPool.play(soundIds[4],1,1,1,0,1);
    }else if(value==6){
        soundPool.play(soundIds[5],1,1,1,0,1);
    }else if(value==7){
        soundPool.play(soundIds[6],1,1,1,0,1);
    }else if(value==8){
        soundPool.play(soundIds[7],1,1,1,0,1);
    }

我有多个级别,不同的声音,但在一个级别不超过4个声音。 使用if else来播放声音。

类似的代码适用于不赞成使用的声音池,而且它的工作正常

1 个答案:

答案 0 :(得分:0)

您已加载多个声音文件,因此您需要等到所有声音文件都加载到soundpool中。

// Load files first
soundIds[0] = soundPool.load(context, R.raw.one, 1);
soundIds[5] = soundPool.load(context, R.raw.stick, 6);
soundIds[3] = soundPool.load(context, R.raw.loss, 4);
soundIds[4] = soundPool.load(context, R.raw.win, 5);

loaded = false;
final int lastLoadedId = soundIds[4];

//Then wait for the onLoadComplete event
soundPool.setOnLoadCompleteListener(new SoundPool.OnLoadCompleteListener() 
{               
      @Override
      public void onLoadComplete(SoundPool soundPool, int sampleId, int status) 
      {
        if(sampleId == lastLoadedId)
        {
            loaded= true;
        }
        // May be you want to add log here to see the IDs of the items loaded
      }
});