android 声音,同时播放声音Android
我正在Android上創(chuàng)建游戲,并且已經(jīng)將這個問題擱置了一段時間,現(xiàn)在又回到了問題上。在我的游戲中,我有背景音樂,槍聲,爆炸聲等,而且我需要能夠同時玩它們。現(xiàn)在,當我在SoundPool類上調(diào)用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中,然后根據(jù)用戶輸入我只想播放聲音。這看起來正確嗎?還是我應該嘗試以其他方式去做?
總結
以上是生活随笔為你收集整理的android 声音,同时播放声音Android的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: jq调用android方法,Androi
- 下一篇: android activity 回调函