我创建了一个 SoundManager 类,它使用 SoundPool 函数来循环播放特定的声音,但是我不知道为什么它不播放声音。

public class SoundManager {
private  SoundPool mSoundPool;
private  HashMap<Integer, Integer> mSoundPoolMap;
private  AudioManager  mAudioManager;
private  Context mContext;

public SoundManager()
{

}

public void initSounds(Context theContext) {
     mContext = theContext;
     mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
     mSoundPoolMap = new HashMap<Integer, Integer>();
     mAudioManager = (AudioManager)mContext.getSystemService(Context.AUDIO_SERVICE);
}

public void addSound(int Index, int SoundID) {
    mSoundPoolMap.put(Index, mSoundPool.load(mContext, SoundID, 1));
}

public void playSound(int index) {
     int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
     mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, 0, 1f);
}

public void playLoopedSound(int index) {
     int streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
     mSoundPool.play(mSoundPoolMap.get(index), streamVolume, streamVolume, 1, -1, 1f);
}

}

正在通过以下方式从我的 Activity 中调用此类:
private SoundManager mSoundManager;
mSoundManager = new SoundManager();
mSoundManager.initSounds(getBaseContext());
mSoundManager.addSound(1, R.raw.sound);
mSoundManager.playSound(1);

最佳答案

显然它不适用于我的 Activity onCreate 但在事件发生时调用它( Intent/按键)它似乎工作

10-08 14:40