我在声音管理器方面遇到了一个模糊的问题。只有我加载的第一个声音有效。第二声没有。
初始化,这里 WRONG_SOUND 不加载。
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);
如果我反转加载,CORRECT_SOUND 不会加载
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);
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 方法
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 实现 OnLoadCompleteListener
@Override
public void onLoadComplete(SoundPool arg0, int arg1, int arg2) {
synchronized (this) {
this.notifyAll();
}
}
我认为这是一个负载问题,所以我添加了一个 OnLoadCompleteListener,但加载的第二个声音仍然无法正常工作。