【发布时间】:2010-04-13 05:09:07
【问题描述】:
我无法使用以下代码同时播放多个声音/哔声。在我的onclicklistener 中我添加了:
public void onClick(View v) {
mSoundManager.playSound(1);
mSoundManager.playSound(2);
}
但这一次只播放一个声音,索引为 1 的声音,然后是索引为 2 的声音。
每当有onClick() 事件时,如何使用此代码同时播放至少两种声音?
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(1, 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);
}
}
【问题讨论】:
-
我认为您在
mSoundPoolMap...mSoundPoolMap.put(1, ...中使用相同索引存储每个声音 -
嗨 Roman,当我必须使用上面的代码时,我将 mSoundPoolMap 方法更改为 mSoundPoolMap.put(index, mSoundPool.load(mContext, SoundID, 1));当我使用并播放声音时,我会这样做 - final SoundManager mSoundManager = new SoundManager(); mSoundManager.initSounds(getBaseContext()); mSoundManager.addSound(1, R.raw.1); mSoundManager.addSound(2, R.raw.2); mSoundManager.addSound(3, R.raw.2);这意味着索引 1,2 和 3 转到 mSoundPoolMap.put()。但即便如此,我也无法同时播放这些声音。请帮忙。