【问题标题】:Soundpool.builder samples no readySoundpool.builder 样本未准备好
【发布时间】:2015-04-13 08:25:43
【问题描述】:

我在 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 by there 值播放声音。

类似的代码适用于不推荐使用的声音池,它正在工作

【问题讨论】:

  • 我检查并 log.i 显示所有 4 种声音都已加载但仍然没有声音。并在 logcat 中说该示例尚未准备好

标签: android soundpool


【解决方案1】:

您已经加载了多个声音文件,因此您需要等到所有声音文件都加载到 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
      }
});

【讨论】:

  • 起初我尝试使用数组检查每个声音文件的加载状态,但后来我注意到soundIds[] 没有按顺序排列,这使得很难追溯每个项目的索引(数组)。如果可能,修复soundIds[] 的排列并将loaded[] 作为一个数组来检查加载状态。如果soundIds[4] 在其他项目之前触发了setOnLoadComplete,则上述代码可能不起作用。
猜你喜欢
  • 2011-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多