I am facing a vague issue with sound manager. Only the first sound that I load, works. The 2nd sound does not.
Initialization, here WRONG_SOUND does not load.
Globals.mSoundManager = new SoundManager();
Globals.mSoundManager.initSounds(getBaseContext());
Globals.mSoundManager.addSound(Globals.CORRECT_SOUND, R.raw.correct);
Globals.mSoundManager.addSound(Globals.WRONG_SOUND, R.raw.wrong);
If I reverse the loading, CORRECT_SOUND does not load
Globals.mSoundManager = new SoundManager();
Globals.mSoundManager.initSounds(getBaseContext());
Globals.mSoundManager.addSound(Globals.WRONG_SOUND, R.raw.wrong);
Globals.mSoundManager.addSound(Globals.CORRECT_SOUND, R.raw.correct);
Constructor of SoundManager
public void initSounds(Context theContext) {
mContext = theContext;
mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
mSoundPool.setOnLoadCompleteListener(this);
mSoundPoolMap = new HashMap<Integer, Integer>();
mAudioManager = (AudioManager) mContext
.getSystemService(Context.AUDIO_SERVICE);
}
addSound method
public synchronized void addSound(int Index, int SoundID) {
mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
synchronized(this) {
try {
this.wait(1000) ;
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
SoundManager implements OnLoadCompleteListener
@Override
public void onLoadComplete(SoundPool arg0, int arg1, int arg2) {
synchronized (this) {
this.notifyAll();
}
}
I thought it was a load issue, so I added a OnLoadCompleteListener, but still the 2nd sound loaded fails to work.
To fix this I had to change the mp3 file, and it worked. Magically the mp3 file works on media player of Android, also on the mac. But for some reason this crazy change to a new mp3 was the only way to fix it. Many SO questions have this solution too.