糖尿病康复,内容丰富有趣,生活中的好帮手!
糖尿病康复 > android 声音 同时播放声音Android

android 声音 同时播放声音Android

时间:2024-03-04 18:42:29

相关推荐

android 声音 同时播放声音Android

我正在Android上创建游戏,并且已经将这个问题搁置了一段时间,现在又回到了问题上。在我的游戏中,我有背景音乐,枪声,爆炸声等,而且我需要能够同时玩它们。现在,当我在SoundPool类上调用play时,当前正在播放的声音被打断,新的声音开始播放。下面是我的SoundManager类及其用法。任何帮助将不胜感激,因为这确实是我需要拥有如此多音效的第一款游戏。谢谢!

public class SoundManager {

private SoundPool mSoundPool;

private HashMap mSoundPoolMap;

private AudioManager mAudioManager;

private Context mContext;

public SoundManager(Context theContext) {

mContext = theContext;

mSoundPool = new SoundPool(4, 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 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);

}

}

…这是我如何使用该类的示例。

SoundManager sm = new SoundManager(this);

sm.addSound(0, R.raw.explosion);

sm.playSound(0);

…因此,使用这种样式,我将所有声音在加载时添加到SoundPool中,然后根据用户输入我只想播放声音。这看起来正确吗?还是我应该尝试以其他方式去做?

如果觉得《android 声音 同时播放声音Android》对你有帮助,请点赞、收藏,并留下你的观点哦!

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。