我想弄清楚如何在应用中加载各种声音。我的声音管理器工作顺利,如下所示:
package com.beeseries.contextclues;
import java.util.HashMap;
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager {
private SoundPool mSoundPool;
private HashMap mSoundPoolMap;
private AudioManager mAudioManager;
private Context mContext;
public void initSounds(Context theContext) {
mContext = theContext;
mSoundPool = new SoundPool(1, AudioManager.STREAM_MUSIC, 0);
mSoundPoolMap = new HashMap();
mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}
public void addSound(int index, int SoundID)
{
mSoundPoolMap.put(index, mSoundPool.load(mContext, SoundID, 1));
}
public void loadWritingSounds(){
mSoundPoolMap.put(0, mSoundPool.load(mContext, R.raw.writing1, 1));
mSoundPoolMap.put(1, mSoundPool.load(mContext, R.raw.writing2, 1));
mSoundPoolMap.put(2, mSoundPool.load(mContext, R.raw.writing3, 1));
mSoundPoolMap.put(3, mSoundPool.load(mContext, R.raw.writing4, 1));
mSoundPoolMap.put(4, mSoundPool.load(mContext, R.raw.writing5, 1));
}
public void playSound(int index)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play((Integer) mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}
public void playLoopedSound(int index)
{
float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
mSoundPool.play((Integer) mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f);
}
}
我的应用程序的开放活动包含以下代码片段,以使声音有效:
public SoundManager mSoundManager;
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.loadWritingSounds();
以下代码播放随机声音(哪些工作,但仅限于开放活动):
int b = randSound1.nextInt((whichSound.length));
mSoundManager.playSound(b);
所以我的困境是我需要能够立即为所有活动加载WritingSounds,因为现在,如果我尝试在另一个活动上播放声音,除非我添加代码片段,否则它将无法工作使声音管理器工作。
我该怎么办??