I am developing an app that is translating phrases from english to another language. I am using ExpandableListView and to that binding data through BaseExpandableListAdapter. Briefly: when one clickes a listitem, a sub item opens where you can see the translation and at the same time a voice is speaking. The problem is that now and then the sound is not played - especially for longer phrases. And what I can see in the logcat is the following:
1) WHEN SOUND IS NOT PLAYING AT ALL...
Sample was not loaded. Waiting for 30 ms. SAMPLE X IS NOT READY
2) WHEN SOUND IS ACTUALLY PLAYED
*Sample was not loaded. Waiting for 30 ms.
So EVEN if the sound is played the logcat tells that "sample was not ready".
Ok, this is the information logcat gives. Another thing is that the probability of failure is bigger for larger soundfiles. Small soundfiles is played for two seconds (approx. 30 KB) and larger about 4 seconds (approx. 60 KB).
Right now I cannot figure out how to solve this issue. I have searched the internet for solutions, especially this site. I have both tried to ....
1) use the OnLoadCompleteListener()
and its NOT WORKING
2) making some kind of while loop.
Not working either
What could I have made wrong. I give the code below. Maybe something embarrasing? Have I implemented the listener in a wrong manner for instance?
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);
}
}
}
});
I think you should change your code as follows:
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);
}
});
It had worked for me.