示例没有准备好的soundpool

Bjö*_*röm 3 android soundpool

我正在开发一个将短语从英语翻译成另一种语言的应用程序.我使用ExpandableListView和通过BaseExpandableListAdapter绑定数据.简而言之:当点击一个listitem时,会打开一个子项目,您可以在其中查看翻译,同时发出语音.问题是偶尔会播放声音 - 特别是对于较长的短语.我在logcat中看到的内容如下:

1)当声音没有播放时......

样品未加载.等待30毫秒.样品X未准备好

2)当声音实际播放时

*未加载样品.等待30毫秒.


因此,即使播放声音,logcat也会告诉"样品没有准备好".

好的,这是logcat给出的信息.另一件事是,对于较大的声音文件,失败的概率更大.小型声音文件播放两秒钟(约30 KB),大约4秒钟(约60 KB).

现在我无法弄清楚如何解决这个问题.我在网上寻找解决方案,尤其是这个网站.我都试过......

1)使用OnLoadCompleteListener()

不工作

2)制作某种while循环.

也不工作

我有什么不对的.我给出下面的代码.也许有些尴尬?我是否以错误的方式实现了侦听器?

Cincerely

expList.setOnGroupExpandListener(new OnGroupExpandListener() {
    public void onGroupExpand(int groupPosition) {

        final int group_position = groupPosition; 
        loaded = false;


        int nmbr_childs = adapter.getChildrenCount(groupPosition);
        if (nmbr_childs == 1) {


            myVoice = soundPool.load(PhraseActivity.this, sound[group_position][0], 2);
            soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
                  @Override
                  public void onLoadComplete(SoundPool soundPool, int sampleId,
                      int status) {
                    loaded = true;
                  } 
            });

            if (loaded) {
                soundPool.play(myVoice, 20, 20, 1, 0, 1f);
            }
            else {
                System.out.println("something wrong with soundpool!");
            }   


        }

        group = groupPosition;
            int len = adapter.getGroupCount();
            for (int i = 0; i < len; i++) {
                if (i != groupPosition) {
                    expList.collapseGroup(i);
                }
            }
        }
    }); 
Run Code Online (Sandbox Code Playgroud)

小智 7

我认为您应该按如下方式更改代码:

 myVoice = soundPool.load(PhraseActivity.this, sound[group_position][0], 2);
        soundPool.setOnLoadCompleteListener(new OnLoadCompleteListener() {
              @Override
              public void onLoadComplete(SoundPool soundPool, int sampleId,
                  int status) {
                    soundPool.play(myVoice, 20, 20, 1, 0, 1f);
              } 
        });
Run Code Online (Sandbox Code Playgroud)

它对我有用.