同时播放Android的声音

DRi*_*FTy 11 java audio android

我在Android上创建了一个游戏,我有点暂时解决这个问题,现在我回到它.在我的游戏中,我有背景节拍,枪声,爆炸......等等,我需要能够同时播放它们.现在,当我在SoundPool类上调用play时,当前正在播放的声音被中断,新的声音开始播放.我的SoundManager类以及使用情况如下.任何帮助都会非常感激,因为这是我第一次需要这么多音效的游戏.谢谢!

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

public SoundManager(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) {
    float streamVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_RING);
    streamVolume = streamVolume / mAudioManager.getStreamMaxVolume(AudioManager.STREAM_RING);

    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);
}
}
Run Code Online (Sandbox Code Playgroud)

......这是我如何使用该类的一个例子.

SoundManager sm = new SoundManager(this);
sm.addSound(0, R.raw.explosion);
sm.playSound(0);
Run Code Online (Sandbox Code Playgroud)

...所以有了这种风格,我在加载时将所有声音添加到SoundPool,然后根据用户输入我只想播放声音.这看起来是否正确?或者我应该尝试以不同的方式解决这个问题?

DRi*_*FTy 16

好吧,我最终确定了这个,以防其他人想知道.问题不在于它不能在同一时间它是,它是唯一能够在这给我的声音是停止和启动印象时间打4周的声音播放多个声音.在构造函数这一行

mSoundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);

需要更改以允许更多流同时播放.因此,通过将第一个参数从4更改为20,您可以同时播放20个声音.哈哈,游戏听起来好多了.希望这有助于某人.