Android“无法加载示例(null)”

时间:2012-10-03 07:40:51

标签: android soundpool

我正面临声音管理者的模糊问题。只有我加载的第一个声音才有效。第二种声音没有。

初始化,这里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,但仍然加载了第二个声音无效。

1 个答案:

答案 0 :(得分:8)

要解决此问题,我必须更改mp3文件,并且它有效。奇怪的是,mp3文件适用于Android的媒体播放器,也适用于Mac。但出于某种原因,这种对新mp3的疯狂改变是解决它的唯一方法。许多SO问题也有这个解决方案。